Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost::static\u visitor中运算符()中的附加参数_C++_Performance_Boost_C++14_Variant - Fatal编程技术网

C++ boost::static\u visitor中运算符()中的附加参数

C++ boost::static\u visitor中运算符()中的附加参数,c++,performance,boost,c++14,variant,C++,Performance,Boost,C++14,Variant,我必须创建boost::variant对象并使用static\u visitor。 不幸的是,我需要额外的参数 什么解决方案更好? 要将此对象作为类的字段,并且当我要使用visitor时,我必须创建实例: class Visitor : public boost::static_visitor<> { public: Visitor(int extra): extra{extra} {} void operator()(const T1&) { /* ...

我必须创建
boost::variant
对象并使用
static\u visitor
。 不幸的是,我需要额外的参数

什么解决方案更好? 要将此对象作为类的字段,并且当我要使用visitor时,我必须创建实例:

class Visitor : public boost::static_visitor<>
{
public:
    Visitor(int extra): extra{extra} {}
    void operator()(const T1&) { /* ... */}
    void operator()(const T2&) { /* ... */}

private:
    int extra;
};
或者一次性使用boost::bind和create Visitor并使用
boost::bind

class Visitor : public boost::static_visitor<>
{
public:
    void operator()(const T1&, int extra) { /* ... */ }
    void operator()(const T2&, int extra) { /* ... */ }
};
什么是更好(更快、更优雅)的解决方案

或者有更好的解决方案吗?

没有比这更优雅的方法了。您可以使用lambdas(如果您的编译器/boost版本足够现代)

“低技术”选项是使用保存状态的结构(第三个示例):

#include <boost/variant.hpp>
#include <iostream>

struct T1{};
struct T2{};

struct Visitor : boost::static_visitor<>
{
    void operator()(T1 const&, int extra) const { std::cout << __PRETTY_FUNCTION__ << " extra:" << extra << "\n"; }
    void operator()(T2 const&, int extra) const { std::cout << __PRETTY_FUNCTION__ << " extra:" << extra << "\n"; }
};

int main() {
    boost::variant<T1, T2> tests[] = { T1{}, T2{} };

    {
        Visitor vis;
        for (auto v: tests)
            apply_visitor([=](auto const& v) { vis(v, 42); }, v);
    }

    {
        auto vis = [vis=Visitor{}](auto const& v) { vis(v, 1); };
        for (auto v: tests)
            apply_visitor(vis, v);
    }


    {
        struct {
            using result_type = void;
            int extra;
            void operator()(T1 const&) const { std::cout << __PRETTY_FUNCTION__ << " extra:" << extra << "\n"; }
            void operator()(T2 const&) const { std::cout << __PRETTY_FUNCTION__ << " extra:" << extra << "\n"; }
        } vis { 99 };

        for (auto v: tests)
            apply_visitor(vis, v);
    }
}
#包括
#包括
结构T1{};
结构T2{};
结构访问者:boost::static\u访问者
{
void运算符()(T1常量和,int额外)常量{std::cout
auto visitor = std::bind(SctpManager::Visitor(), std::placeholders::_1, extra);
boost::apply_visitor(visitor, t);
#include <boost/variant.hpp>
#include <iostream>

struct T1{};
struct T2{};

struct Visitor : boost::static_visitor<>
{
    void operator()(T1 const&, int extra) const { std::cout << __PRETTY_FUNCTION__ << " extra:" << extra << "\n"; }
    void operator()(T2 const&, int extra) const { std::cout << __PRETTY_FUNCTION__ << " extra:" << extra << "\n"; }
};

int main() {
    boost::variant<T1, T2> tests[] = { T1{}, T2{} };

    {
        Visitor vis;
        for (auto v: tests)
            apply_visitor([=](auto const& v) { vis(v, 42); }, v);
    }

    {
        auto vis = [vis=Visitor{}](auto const& v) { vis(v, 1); };
        for (auto v: tests)
            apply_visitor(vis, v);
    }


    {
        struct {
            using result_type = void;
            int extra;
            void operator()(T1 const&) const { std::cout << __PRETTY_FUNCTION__ << " extra:" << extra << "\n"; }
            void operator()(T2 const&) const { std::cout << __PRETTY_FUNCTION__ << " extra:" << extra << "\n"; }
        } vis { 99 };

        for (auto v: tests)
            apply_visitor(vis, v);
    }
}
void Visitor::operator()(const T1&, int) const extra:42
void Visitor::operator()(const T2&, int) const extra:42
void Visitor::operator()(const T1&, int) const extra:1
void Visitor::operator()(const T2&, int) const extra:1
void main()::<unnamed struct>::operator()(const T1&) const extra:99
void main()::<unnamed struct>::operator()(const T2&) const extra:99