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);
}
};
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); }
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;
}
Why is partial specialization of a nested class template allowed, while complete isn't?
My guess as to why this happens: complete specializations are no longer "template classes/functions", they are are "real" classes/methods, and get to have real (linker-visible) symbols. But for a completely-specialized template inside a partially-specialized one, this would not be true.
Probably this decision was taken just to simplify the life of compiler-writers (and make life harder for coders, in the process :P ).
Explicit specialization in non-namespace scope error... Desperately need help
In this case overloading should suffice instead of specializing:
template <typename T> class Data_T
{
public:
template<typename U> static bool IsEqual(const U& a, const U& b)
{
return a == b;
}
static bool IsEqual(const MyData& a, const MyData& b)
{
return MyDataTypeIsEqual (a, b);
}
template<typename U> bool IsRangeEqual(const U& a, const U& b, const U& delta)
{
return (a >= b) ? (a - b <= delta) : (b - a <= delta);
}
bool IsRangeEqual(const MyData & a, const MyData & b, const MyData & accuracy)
{
return MyDataTypeIsRangeEqual (a, b, accuracy);
}
};
Edit regarding update:
While for me the conversion from float
to float __vector__
already fails, you seem to have a typo of:
template<typename U> static bool IsEqual(const T& a, const T& b)
vs.:
template<typename U> static bool IsEqual(const U& a, const U& b)
I fixed the above code accordingly.
Related Topics
How to Use Clang++ with -Std=C++11 -Weverything -Werror
How to Write an Adl-Enabled Trailing Return Type, or Noexcept Specification
C++11 Constexpr Flatten List of Std::Array into Array
Opening a Window That Has No Title Bar with Win32
What Is the Modern, Correct Way to Do Type Punning in C++
Why Can't I Static_Cast Between Char * and Unsigned Char *
Using Zeromq Together with Boost::Asio
Implementing the Derivative in C/C++
Aligned_Storage and Strict Aliasing
Is Std::String Thead-Safe with Gcc 4.3
Visual Studio Project Out of Date
Passing Member Function Pointer to Member Object in C++
Reading and Writing to Usb (Hid) Interrupt Endpoints on MAC
Does an Unused Member Variable Take Up Memory
Qt "Private Slots:" What Is This
C++ Check If Statement Can Be Evaluated Constexpr
How to Edit and Re-Build the Gcc Libstdc++ C++ Standard Library Source
Are There Any Issues with Allocating Memory Within Constructor Initialization Lists