首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类模板+函数模板

类模板+函数模板
EN

Stack Overflow用户
提问于 2012-12-10 23:32:44
回答 2查看 83关注 0票数 0

当我尝试创建一个模板类时,如下所示:

代码语言:javascript
复制
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 ..

我得到了以下错误:

代码语言:javascript
复制
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)'

当我在类定义中实现这些专门化时,所有这些都是立即编译的。所以我猜我把模板语法弄错了。但是很难找到混合了类和函数模板并带有特殊化的示例。所以我最终来到了这里,希望有人能给我一个好的提示。

EN

回答 2

Stack Overflow用户

发布于 2012-12-10 23:41:12

看起来你不需要把“模板<>”放在你的专业之上。

如果我删除它们,一切都编译得很好(查找不要在家中尝试这个)

代码语言:javascript
复制
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 ..
票数 0
EN

Stack Overflow用户

发布于 2015-09-25 19:24:57

正如您所说,您不需要template<>,因为您没有专门化该函数。

你所做的是函数重载!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13804007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档