I have a variant class which uses some function template specialization for getting and setting different types which compiled and worked fine with Visual Studio 2010. However this code was located in a common solution which also needs to compile on redhat, ubuntu, etc.
I received an error along the lines of explicit specialization in non-namespace scope. I figured the easy fix was to simply define my specializations outside the class with scope qualifier for the class in the same namespace.
However now I'm getting errors that the specialization occurs after the instantiation as other methods of the class for converting from various types are using this template within the class.
So what is the correct way to do something like this:
namespace Example
{
class CSomeVariant
{
public:
bool toString(std::string& out)
{
return get(out);
}
private:
template <typename T>
bool get(T& val)
{
try {
val = boost::any_cast<T>(m_stored);
}
catch (...) {
return false;
}
return true;
}
boost::any m_stored;
};
template<>
bool CSomeVariant::get(std::string& val)
{
try {
if (m_stored.type() != typeid(std::string))
val = convertToString();
else
val = boost::any_cast<std::string>(m_stored);
}
catch(...) {
return false;
}
return true;
}
}
Note: This is not the actual code but I believe it shows the problem.