An interesting consequence of decay related rules of pointers
and references is that seemingly correct C++ code fails to
compile.
We have a max() template with pretty obvious implementation
for basic types.
template < typename T >
T & max (T &a,T &b);
but,
max("APPLE","PEAR");
Gives an error, because
type of "APPLE" is const char [6]
type of "PEAR" is const char [5]
T can't be "const char [6]" AND "const char [5]"
at the same time!! No array-to-pointer decay occurs here.
Above thing will work for following declaration of max
template < typename T >
T max (T a, T b);
Here T is char *!!
and references is that seemingly correct C++ code fails to
compile.
We have a max() template with pretty obvious implementation
for basic types.
template < typename T >
T & max (T &a,T &b);
but,
max("APPLE","PEAR");
Gives an error, because
type of "APPLE" is const char [6]
type of "PEAR" is const char [5]
T can't be "const char [6]" AND "const char [5]"
at the same time!! No array-to-pointer decay occurs here.
Above thing will work for following declaration of max
template < typename T >
T max (T a, T b);
Here T is char *!!
Comments
Please look at the following website
thanks you!
gclub