The following code fails to compile because of error: redefinition of ‘template<class Integer, class> void func(Integer)’
#include <iostream>
#include <type_traits>
template<typename Float, typename = typename
std::enable_if<std::is_floating_point<Float>::value>::type>
void func(Float floatVal)
{
std::cerr << "float: " << floatVal << "\n";
}
template<typename Integer, typename = typename
std::enable_if<std::is_integral<Integer>::value>::type>
void func(Integer integer)
{
std::cerr << "integral: " << integer << "\n";
}
int main()
{
func(32.4246);
func(144532);
}
But the two functions will clearly have different signatures on template instantiation. So why is can't this compile?
Please note: I do know how to fix this: just adding another dummy template parameter to one of the functions, e.g. typename=void
, will work, like here
template<typename Integer, typename dummy=void, typename = typename
std::enable_if<std::is_integral<Integer>::value>::type>
void func(Integer integer){}
But the question is why do I have to do this?