gpt4 book ai didi

c++ - boost::apply_visitor 的返回类型是什么(延迟版本)

转载 作者:太空狗 更新时间:2023-10-29 21:25:52 34 4
gpt4 key购买 nike

在下面的代码中,我将延迟 apply_visitor 的结果存储在 auto 变量中。我可以使用什么类型来代替 auto?是否可以使用 std::function

#include "boost/variant/variant.hpp"
#include "boost/variant/apply_visitor.hpp"

using namespace std;
using namespace boost;

class times_two_generic
: public boost::static_visitor<>
{
public:

template <typename T>
void operator()( T & operand ) const
{
operand += operand;
}

};

int main(int argc, char **argv)
{
variant<int, string> v = 5;
times_two_generic visitor;

cout << v << endl;

apply_visitor(visitor)(v); // v => 10
auto appliedVisitor = apply_visitor(visitor);
appliedVisitor(v); // v => 20

cout << v << endl;
return 0;
}

最佳答案

根据 http://www.boost.org/doc/libs/1_52_0/doc/html/boost/apply_visitor.html , 实际类型是 apply_visitor_delayed_t<times_two_generic> .

因为它只是另一个函数对象,你也可以使用 std::function ,但这比使用真实类型的成本更高。

关于c++ - boost::apply_visitor 的返回类型是什么(延迟版本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13249204/

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