I'm having trouble wrestling with C++'s syntax for template specializations outside of the class definition. I have this class:
template <class T>
class Foo {
public:
template <int U>
std::string bar();
private:
T m_data;
};
How can I specialize bar()
for any T
and a specific U
?
I was expecting:
template <class T> template <>
std::string Foo<T>::bar<1>() {
return m_data.one;
}
But I get:
error: invalid explicit specialization before ‘>’ token
template <class T> template <>
^
error: enclosing class templates are not explicitly specialized
I could also try:
template <class T> template <int>
std::string Foo<T>::bar<1>() {
return m_data.one;
}
but then I get:
error: non-class, non-variable partial specialization ‘bar<1>’ is not allowed
std::string Foo<T>::bar<1>() {
^