#include <iostream>
#include <string>
template<typename U>
struct A
{
template<typename... Ts> auto func();
template<> auto func<int>();
};
template<typename U>
template<>
auto
A<U>::func<int>() { return std::string{"foo"}; }
int main()
{
A<float> a{};
std::cout << a.func<int>() << std::endl;
}
This does not work as specialization of template members of template classes is not possible unless you also specialize the class. (I read about that.)
But if you move the definition of the member specialization to the class definition, it does work:
#include <iostream>
#include <string>
template<typename U>
struct A
{
template<typename... Ts> auto func();
template<> auto func<int>() { return std::string{"foo"}; }
};
int main()
{
A<float> a{};
std::cout << a.func<int>() << std::endl;
}
I'm not sure I fully understand why. Also, while it is working with clang, it does not compile with gcc. So which one is right?
But my real question is, assuming clang is getting it right, why this again is not working:
#include <iostream>
#include <string>
template<typename U>
struct A
{
template<typename... Ts> auto func();
template<> auto func<U>() { return std::string{"foo"}; }
};
int main()
{
A<int> a{};
std::cout << a.func<int>() << std::endl;
}
It is a different error, not specialization of member of unspecialized template, but rather the complaint is that func<int>
with deduced return type cannot be used before it is defined.