template function parsing - what's going on here?

1
2
3
4
5
6
7
8
9
10
11
12
13
template< typename TYPE >
void Foo( const TYPE & foo ) {}

int main ( void ) {

	const int* bar;
	
	Foo<int*>( bar );
	// error: no matching function for call to ‘Foo(const int*&)’
	// WHAT? ... lies and deceit!
	
	return 0;
}

closed account (o1vk4iN6)
Might be more noticeable with different syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

int const * a;
const int * 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 ) ?
closed account (o1vk4iN6)
1
2
template< typename T >
void Foo( const T* bar ) { }


Would need to handle that case.
Templates aren't as powerful as I have been led to believe.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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
    const int c = i ; foo(c) ; // foo( const T& ) with T == int
    int* p = &i ; foo(p) ; // foo( T& ) with T == pointer to int
    const int* pc = p ; foo(pc) ; // foo( T& ) with T == pointer to const int
    int* const cp = p ; foo(cp) ; // foo( T* const& ) with T == int
    const int* 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
}
closed account (o1vk4iN6)
caibber wrote:
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.
Topic archived. No new replies allowed.