0

when I tried to create a template class as follows:

template <typename TList>
class Variant
{
public :
    std::string toString(); // var.toString()

    template<typename T>
    std::string toString(); // var.toStrint<int>();

    protected:
    template <typename T>
    std::string toString(T v); // internal Specialization

    template <>
    std::string toString(int v); // internal Specialization

    template <typename T>
    const T & get() 
    {
        std::size_t  type_index = TypeListNamespace::IndexOf<TList, T>::value ;

        if ( type_index == max_num_of_t  || type_index != _type_index)
            throw std::bad_cast() ;  

        void * ptr = (void*) &_variant_holder;
        T * vptr = reinterpret_cast<T *>(ptr);
        return *vptr; 
    }
};

// CPP FILE:

template <typename TList>
std::string Variant<TList>::toString ()
{
    // var.toString()
}

template<typename TList>
template<typename T>
std::string Variant<TList>::toString ()
{               
    return toString( get<T>() );  
}

template<typename TList>
template<typename T>
std::string Variant<TList>::toString (T v)
{                
    // no default toString method.
    return "";
}

template<typename TList>
template<>       
std::string Variant<TList>::toString (int v)
{                
    // Specialized toString for int values:
    return Core::Utility::formatString("%i", v );
}

.. other specializations ..

I got the following error:

error C2244: 'Core::Variant<TList>::toString': unable to match function definition to an existing declaration
2>          Definition
2>          'std::string Core::Variant<TList>::toString(int)'
2>          Available Deklarations
2>          'std::string Core::Variant<TList>::toString(T)'
2>          'std::string Core::Variant<TList>::toString(void)'
2>          'std::string Core::Variant<TList>::toString(void)'

When I had these specializations inside the class definition all compiled right away. So I guess I made something wrong with the template syntax. But its hard to find examples with a mix of class and function templates with specialization. So I ended up here hoping for someone who has a good hint for me.

hhamm
  • 1,511
  • 15
  • 22

2 Answers2

0

It seems that you dont have to put "template <>" above your specializations.

If I remove them, everything compiles fine ( look for DON'T TRY THIS AT HOME )

template <typename TList>
class Variant
{
public :
    std::string toString(); // var.toString()

    template<typename T>
    std::string toString(); // var.toStrint<int>();

    protected:
    template <typename T>
    std::string toString(T v); // internal Specialization

    // DON'T TRY THIS AT HOME: template <>
    std::string toString(int v); // internal Specialization

    template <typename T>
    const T & get() 
    {
        std::size_t  type_index = TypeListNamespace::IndexOf<TList, T>::value ;

        if ( type_index == max_num_of_t  || type_index != _type_index)
            throw std::bad_cast() ;  

        void * ptr = (void*) &_variant_holder;
        T * vptr = reinterpret_cast<T *>(ptr);
        return *vptr; 
    }
};

// CPP FILE:

template <typename TList>
std::string Variant<TList>::toString ()
{
    // var.toString()
}

template<typename TList>
template<typename T>
std::string Variant<TList>::toString ()
{               
    return toString( get<T>() );  
}

template<typename TList>
template<typename T>
std::string Variant<TList>::toString (T v)
{                
    // no default toString method.
    return "";
}

template<typename TList>
// DON'T TRY THIS AT HOME: template<>       
std::string Variant<TList>::toString (int v)
{                
    // Specialized toString for int values:
    return Core::Utility::formatString("%i", v );
}

.. other specializations ..
hhamm
  • 1,511
  • 15
  • 22
0

As you said, you don't need the template<>, because you didn't specialize the function.

What you did is function overloading!

Tal
  • 347
  • 2
  • 16