【问题标题】:Template parent class with no type parameter没有类型参数的模板父类
【发布时间】:2016-08-26 05:38:07
【问题描述】:

boost 文档中,有:

class times_two_visitor
    : public boost::static_visitor<>
{
public:
    void operator()(int & i) const
    {
        i *= 2;
    }
    void operator()(std::string & str) const
    {
        str += str;
    }

};

类声明中boost::static_visitor是什么意思? 它看起来像模板专业化,但没有任何特定类型。所以我很困惑。

【问题讨论】:

    标签: c++ templates boost


    【解决方案1】:

    您可以使用模板、类模板以及函数模板,如果模板具有所有模板参数的默认值,则使用&lt;&gt; 作为模板参数。

    例如

    template <typename T = int> struct Foo {};
    
    Foo<double> f1;  // Explicitly specified template paramter
    Foo<> f2;        // Default template parameter, int, is used.
    

    template <typename T1 = int, typename T2 = double> struct Bar {};
    
    Bar<float> b1;  // T1 = float, T2 = double
    Bar<> b2;       // T1 = int, T2 = double
    

    附言

    我对@9​​87654326@ 的使用感到惊讶,因为that class template 似乎没有模板参数的默认值。

    【讨论】:

    • header中有模板类型的默认参数
    • @DuffyChan,感谢您提供的信息。很遗憾,头文件和文档不匹配。
    猜你喜欢
    • 2022-01-09
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多