gpt4 book ai didi

c++ - 没有类型参数的模板父类

转载 作者:行者123 更新时间:2023-11-30 05:23:28 25 4
gpt4 key购买 nike

关于 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<>是什么意思?它看起来像一个模板特化,但没有任何特定类型。所以我很困惑。

最佳答案

您可以使用模板、类模板以及函数模板,使用 <>对于模板参数,如果模板具有所有模板参数的默认值。

例如

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

附言

我对 boost::static_visitor<> 的使用感到惊讶自 that class template似乎没有模板参数的默认值。

关于c++ - 没有类型参数的模板父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39158970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com