template function specialization

Hello, forum-folks!

I'm new at this forum.
I use gnu C++ compiler.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

template<class T>
void fun(T a)
{ std::cout << "fun(T) a: " << a << "\n"; }

template<class T>
void fun(T *a)
{ std::cout << "fun(T *) a: " << a << "\n"; }

template<>
void fun<>(char *a)
{ std::cout << "fun(char *) a: " << a << "\n"; }

int main()
{ fun(new char('X')); }


This code works just fine. All is good and okey.
But, if i change the last template specialization
template<>
void fun<>(char *a)

to
template<>
void fun<char *>(char *a)

then the second template:
template<class T>
void fun(T *a)
will be called from main

and if I remove angle braces (and all between then)
from my last template all will be good again.
(will be called the third template)


My question is:
Why in this example fun(T *a) is called instead
fun<char *>(char *a).
I think that fun<char *>(char *a) is more suitable in this case.
???

Thank you all for your time! I will be waiting for your answers.

and sorry me please for my poor English.
Perhaps this example main will clarify it for you:

1
2
3
4
5
6
7
int main()
{
    char* p = new char( 'x' );
    fun( *p );
    fun( p );
    fun( &p );
}


Output:

1
2
3
fun(T) a: x
fun(char *) a: x
fun(T *) a: 0xbff738a0


ie, T = char*, so the second fun() accepts only pointers-to-pointers-to-somethings.
Topic archived. No new replies allowed.