I have a template function void foo(const T&)
. I need specialized implementations for [T = int]
and [T = const char*]
.
#include <iostream>
template<class T>
void foo(const T& arg)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
std::cout << arg << std::endl;
}
template<>
void foo(const int& arg)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
std::cout << arg << std::endl;
}
int main(int argc, char *argv[])
{
int i = 42;
const char *bar = "xyz";
foo(i);
foo(bar);
return 0;
}
The specialization for [T = int]
works, the output is:
void foo(const T&) [with T = int]
42
void foo(const T&) [with T = const char*]
xyz
But, it won't compile if I try to specialize for [T = const char*]
like so:
template<>
void foo(const char*& arg)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
std::cout << arg << std::endl;
}
17:6: error: template-id 'foo<>' for 'void foo(const char*&)' does not match any template declaration
What has me puzzled is that it appears to correctly deduce [T = const char*]
when no specialization is present, but complains if I try to implement one.
Any ideas where I'm going wrong?