Explicit Specialization of Function Templates Help

why would I need it if I could just make a non template function? I'm confused.
Because a non=template function cannot overload a template function.
If you do that you will not get the expected output if you explicitly choose to call the template function.

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

template <class T>
bool foo(T value)
{
	return true;
}

bool foo(int value)
{
	return false;
}

int main()
{
	std::cout << foo(5) << std::endl;
	std::cout << foo<>(5) << std::endl;
	std::cout << foo<int>(5) << std::endl;	
}
0
1
1

Most people would expect that these three function calls give the same result.
> why would I need it if I could just make a non template function? I'm confused.

In general, specializing a function template is a bad idea; prefer providing non-template overloads of the function instead.

This was discussed in this thread: http://www.cplusplus.com/forum/beginner/100227/


Topic archived. No new replies allowed.