I just tried the equivalent in g++ 4.5 C++0x mode for char arrays and it won't let me both define
template <typename T>void moresilly(const T v[],const char *description)
AND
template <typename T>void moresilly(const T *v,const char *description)
it claims both are the same type.
I had a function:
template <typename T>void silly(const T & v,const char *description)
{
cout<<"size of "<<description<<" is "<< sizeof(T)<<endl;
moresilly(v,description);
}
It correctly gets the size of an array if passed and of a pointer if passed, but I can't use moresilly to distinguish between pointer and array, so I can't tell a 4 character array from pointer to n characters.
It might work, sort of, to have templates on T[1],T[2], T[3] etc. but there's already a post saying that different compilers handle that (or some similar case) differently and that gnu prefers a pointer match in C++11.
... added later:
After some experiment I found something that works in g++ 4.5
template <typename T,size_t L>void moresilly(const T (&v)[L],const char *description)
{
cout<<description<<" is an array"<<endl;
}
template <typename T>void moresilly(const T *v,const char *description)
{
cout<<description<<" is a pointer"<<endl;
}
template <typename T>void moresilly(const T v,const char *description)
{
cout<<description<<" is a raw value"<<endl;
}
template <typename T>void silly(const T & v,const char *description)
{
cout<<"size of "<<description<<" is "<< sizeof(T)<<endl;
moresilly(v,description);
}
with the following works properly
silly("12345","immediate string of 5 characters plus zero");
silly((const char *)"12345","immediate constant char pointer of 5 characters plus zero");
char testarray[]="abcdef";
silly(testarray,"char array of 6 characters plus zero");
const char testarray2[]="abcdefg";
silly(testarray2,"const char array of 7 characters plus zero");
Note that if the first function is defined with "const T v[L]" instead of "const T (&v)[L]" it doesn't work, never matching anything.
So I solved your problem but don't expect this to work in other versions of the compiler including future ones. This is why I hate c++. Somehow the definition of the language is so unclear that compilers are full of unstable edge cases.
This is a useful trick though, I may use it.