Re: template function issue



Thanks Doug,


Why GetArrayLength(const T(&arr)[size]) works, but

GetArrayLength(const T(arr)[size]) -- I removed & does not work?


regards,
George

"Doug Harrison [MVP]" wrote:

On Wed, 12 Dec 2007 05:10:01 -0800, George
<George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

I do not quite understand why arr is a reference to an array type?

The parameter "arr" has the type, "reference to array of 'size' T". To
understand why, you have to some basic knowledge of the declaration syntax.
Do you have a specific question about the syntax?

In GetArrayLength(const T(&arr)[size]), I have tried to use,

typeid(arr).name(),

and the result is,

char const(*) [12]

You mean char const(*) [12] is the same as reference to an array type?

No, that means, "pointer to array of 12 const char", and it's very
different. For example, the size of that type is sizeof(pointer), while
sizeof(arr) is the size of the referent, i.e. the array, i.e.
size*sizeof(T).

I am confused. :-)

The type_info::name function doesn't have to give useful information, so it
should not be relied upon to explore the type system. It would be a
problem, though, if typeid(arr) == typeid(ptr), where ptr has the pointer
type you introduced above.

--
Doug Harrison
Visual C++ MVP

.



Relevant Pages

  • Re: type of "string"
    ... conversion of string literals to char* is allowed. ... No, the type of a string literal is not "array of const char", it's ...
    (comp.lang.c.moderated)
  • Re: String literal and const
    ... string literals *cannot* be defined. ... array of type "array N of char" (where N is one more than the number ... char", not "array N of const char". ...
    (comp.lang.c)
  • Re: strange behaviour
    ... array as an argument instead of a double pointer. ... Technically it behaves exactly like char **s2, ... unsigned int const dst_buffer_length, ... void array_truncate; ...
    (comp.lang.c)
  • Re: template function issue
    ... you have to some basic knowledge of the declaration syntax. ... You mean char const(*) is the same as reference to an array type? ...
    (microsoft.public.vc.language)
  • Re: Pointers
    ... > One way to see the declaration syntax is that you ... > declare expression that evaluate to a certain type, char above. ... We hide pointers all the ... and there is no reason not to do the same things, ...
    (comp.lang.c)