I have a class template and a macro that allows me to get the name of a type into a string (taken from this SO answer):
template <typename T>
struct TypeName
{
static const char* get()
{
return typeid(T).name();
}
};
#define ENABLE_TYPENAME(A) template<> struct TypeName<A> { static const char *get() { return #A; }};
I am trying to implement this into a class template like this:
template <typename T>
class TextureIcon : public Icon<TextureIcon<T>>
{
ENABLE_TYPENAME(T)
public:
static void setTextureFile(std::string&& filePath)
{
s_texture_ = loadFromFile<sf::Texture>(filePath, TypeName<T>::get());
}
private:
static std::unique_ptr<sf::Texture> s_texture_;
};
This is currently giving me two errors:
explicit specialization; 'TypeName<T>' has already been instantiated
'TypeName<T>': cannot specialize template in current scope
I assume that these two are related to the same problem, and that the problem stems from template specialization of a class template located outside the current scope, but beyond that I don't really know.
I've found a few questions on SO that seem to revolve around the same problem:
Explicit specialization in non-namespace scope
C++ syntax for explicit specialization of a template function in a template class
But I can't seem to work out how to apply the solutions presented under those questions to my code, as both of them deal with specializations of method templates in the current scope, not specializations for class templates located outside the current scope.