template< typename TYPE >
void Foo( const TYPE & foo ) {}
int main ( void ) {
constint* bar;
Foo<int*>( bar );
// error: no matching function for call to ‘Foo(const int*&)’
// WHAT? ... lies and deceit!
return 0;
}
intconst * a;
constint * b; // same type as a
template< typename T >
void foo( T const & foo ) { }
// with template parameter as int*
void foo( int * const & foo ) { }
int * const c; // not the same as a and b
if in a template (const T & foo) is the same as (T const & foo)... then... how do I use that template with type ( reference to pointer to const int ) ?
#include<iostream>
template< typename T > void foo( T& ) { std::cout << "foo( T& )\n" ; }
template< typename T > void foo( const T& ) { std::cout << "foo( const T& )\n" ; }
template< typename T > void foo( T* const& ) { std::cout << "foo( T* const& )\n" ; }
template< typename T > void foo( const T* const& ) { std::cout << "foo( const T* const& )\n" ; }
int main()
{
int i = 0 ; foo(i) ; // foo( T& ) with T == int
constint c = i ; foo(c) ; // foo( const T& ) with T == int
int* p = &i ; foo(p) ; // foo( T& ) with T == pointer to int
constint* pc = p ; foo(pc) ; // foo( T& ) with T == pointer to const int
int* const cp = p ; foo(cp) ; // foo( T* const& ) with T == int
constint* const cpc = p ; foo(cpc) ; // foo( const T* const& ) with T == int
foo(+i) ; // foo( const T& ) with T == int
foo(+p) ; // foo( T* const& ) with T == int
foo(+pc) ; // foo( const T* const& ) with T == int
foo(+cp) ; // foo( T* const& ) with T == int
foo(+cpc) ; // foo( const T* const& ) with T == int
}
Templates aren't as powerful as I have been led to believe.
If it worked the way you thought it did, how would you do the opposite then ? In most cases you probably don't want to use the same template function when dealing with a pointer, so it is plenty powerful you are just using it wrongly.