boost :: static_visitor multivisitor非变量参数

时间:2014-03-01 17:32:36

标签: c++ c++11 c++14 boost-variant

当应用多用户时,是否有任何廉价的方法来传递非变体类型的参数以及变量类型的参数?

我所说的“昂贵的方式”是指:

#include <boost/variant.hpp>

#include <iostream>

#include <cstdlib>

struct A {};
struct B {};
enum class C { X, Y };

std::ostream &
operator << (std::ostream & out, C const c)
{
    switch (c) {
        case C::X : {
            return out << "C::X";
        }
        case C::Y : {
            return out << "C::Y";
        }
        default : {
            break;
        }
    }
    throw std::runtime_error("unknown C value");
}

using V = boost::variant< A, B >;

struct S
    : boost::static_visitor<>
{

    void
    operator () (C const c, A const &) const
    {
        std::cout << c << " A" << std::endl;
    }

    void
    operator () (C const c, B const &) const
    {
        std::cout << c << " B" << std::endl;
    }

};

int main()
{
    V const a = A{};
    V const b = B{};
    using VC = boost::variant< C >;
    VC const x = C::X;
    VC const y = C::Y;
    S const s;
    boost::apply_visitor(s, x, a);
    boost::apply_visitor(s, y, a);
    boost::apply_visitor(s, x, b);
    boost::apply_visitor(s, y, b);
    return EXIT_SUCCESS;
}

另一种昂贵的方法是使用所需类型的文件(或对所需类型的引用)创建非静态访问者,并为每个非变体类型的值集合构建此类访问者的实例每次

0 个答案:

没有答案