Problem of template

S

swh

Hello.I'm start to write some templates,but I have some questions about
them.

#include <iostream>
using namespace std;

template<typename T> void f(T const& a) {
cout << "Primary" << endl;
}

void f(int const& a) {
cout << "Non-template" << endl;
}

template<> void f(int const& a) {
cout << "Full" << endl;
}

int main() {
f(1.0); // line 1
f(1); // line 2
f<>(1); // line 3
}

The result is:
Primary
Non-template
Non-template

I don't understand the third line in main().I have specified the angle
brackets after the f,
but the resulting call is the Non-template version.Why?
 
V

Victor Bazarov

swh said:
Hello.I'm start to write some templates,but I have some questions
about them.

#include <iostream>
using namespace std;

template<typename T> void f(T const& a) {
cout << "Primary" << endl;
}

void f(int const& a) {
cout << "Non-template" << endl;
}

template<> void f(int const& a) {
cout << "Full" << endl;
}

int main() {
f(1.0); // line 1
f(1); // line 2
f<>(1); // line 3
}

The result is:
Primary
Non-template
Non-template

I don't understand the third line in main().I have specified the angle
brackets after the f,
but the resulting call is the Non-template version.Why?

I can only speculate that it's a bug in the compiler. Have you
tried a different compiler?

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,219
Latest member
KristieKoh

Latest Threads

Top