I am trying to call a template class member function specialization that returns a value from within the class's constructor, and I cannot seem to find the correct syntax (if it exists). Below is my code, and, below that, the error messages from the compiler (not the linker).
#include <iostream>
class A {};
class B {};
template <typename T>
class C
{
public:
C()
{
std::cout << "C constructed." << std::endl;
std::cout << name() << std::endl;;
}
template constexpr const char * name();
};
template <> const char * C<A>::name() { return "You got an A."; }
template <> const char * C<B>::name() { return "You got a B."; }
int main()
{
C<A> c_a;
C<B> c_b;
return 0;
}
Error messages: g++ -std=c++11 -o t1 t1.cpp t1.cpp:19:18: error: expected ‘<’ before ‘constexpr’ template constexpr const char * name(); ^ t1.cpp:22:26: error: template-id ‘name<>’ for ‘const char* C::name()’ does not match any template declaration template <> const char * C::name() { return "You got an A."; } ^ t1.cpp:22:37: note: saw 1 ‘template<>’, need 2 for specializing a member function template template <> const char * C::name() { return "You got an A."; } ^ t1.cpp:23:26: error: template-id ‘name<>’ for ‘const char* C::name()’ does not match any template declaration template <> const char * C::name() { return "You got a B."; } ^ t1.cpp:23:37: note: saw 1 ‘template<>’, need 2 for specializing a member function template template <> const char * C::name() { return "You got a B."; }
I have searched and found many discussions about code that elicits this error message, but neither the cases nor the advice seem to be close enough to be relevant. Also, if I do not try to return anything from the specialized member function--if it has return type void and simply prints to cout--then it works as I would expect, i.e., I see the printed text with the right values.
Is it possible to do what I am trying to do? If so, how? Thanks!
name()
function isn't itself a template, so why thetemplate
? – Bo Persson