Gcc Error: Explicit Specialization in Non-Namespace Scope

Explicit specialization in non-namespace scope

VC++ is non-compliant in this case - explicit specializations have to be at namespace scope. C++03, §14.7.3/2:

An explicit specialization shall be declared in the namespace of which the template is a member, or, for member templates, in the namespace of which the enclosing class or enclosing class template is a member.

An explicit specialization of a member function, member class or static data member of a class template shall be declared in the namespace of which the class template is a member.

Additionally you have the problem that you can't specialize member functions without explicitly specializing the containing class due to C++03, §14.7.3/3, so one solution would be to let Verify() forward to a, possibly specialized, free function:

namespace detail {
template <typename TL> void Verify (int, int[]) {}
template <> void Verify<int>(int, int[]) {}
}

template<typename T> class CConstraint {
// ...
template <typename TL> void Verify(int position, int constraints[]) {
detail::Verify<TL>(position, constraints);
}
};

GCC error: explicit specialization in non-namespace scope

You just have to move your specializations of the member templates outside of the class body.

class VarData
{
public:
template < typename T > bool IsTypeOf (int index) const
{
return IsTypeOf_f<T>::IsTypeOf(this, index);
}
};

template <> bool VarData::IsTypeOf < int > (int index) const
{
return false;
}

template <> bool VarData::IsTypeOf < double > (int index) const
{
return false;
}

OpenGL -- Explicit specialization in non-namespace scope -- Function templates

The error says that it wants you to provide the specialization at a namespace scope. This compiles:

class VertexBufferLayout{
std::vector<VertexBufferElement> m_Elements;
public:
VertexBufferLayout(){}

template< typename T >
void Push(GLuint count){}
};

template<>
void VertexBufferLayout::Push<GLfloat>(GLuint count){
m_Elements.push_back({ GL_FLOAT, count, GL_FALSE });
}
//...

Alternatively, you can use regular overloading to avoid specialization:

class VertexBufferLayout{
std::vector<VertexBufferElement> m_Elements;
public:
VertexBufferLayout(){}

template< typename T >
void Push(GLuint count){ Push_(count, (T*)0); }

void Push_(GLuint count, GLfloat*){
m_Elements.push_back({ GL_FLOAT, count, GL_FALSE });
}
//...
};

C++: error explicit specialization in non-namespace scope

One way forget templates, overload:

Data& set(T1 v) { /*...*/ }

but here is a trick which I use sometimes

you can specialize class template within class:

class {
template<typename T>
struct function_ {
static void apply(T);
};

template<>
struct function_<int> {
...
};

template<typename T>
void function(T t) { return function_<T>::apply(t); }


Related Topics



Leave a reply



Submit