Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.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::variant visitor是a类需求吗?_C++_Boost Variant_Apply Visitor - Fatal编程技术网

C++ boost::variant visitor是a类需求吗?

C++ boost::variant visitor是a类需求吗?,c++,boost-variant,apply-visitor,C++,Boost Variant,Apply Visitor,我是否需要在boost::variant中使用visitor类,例如类visitor:public boost::static\u visitor 如果没有,是否有理由不使用访客?是否有理由选择访客课程 我问这个问题是因为visitor类对于boost::variant的使用是一个多余的方面。您不必强制使用visitor,您可以使用get()完美地查询底层类型 这导致了这样的代码: int foo(boost::variant<int, std::string, Bar> const

我是否需要在boost::variant中使用visitor类,例如
类visitor:public boost::static\u visitor

如果没有,是否有理由不使用访客?是否有理由选择访客课程


我问这个问题是因为visitor类对于boost::variant的使用是一个多余的方面。

您不必强制使用visitor,您可以使用
get()
完美地查询底层类型

这导致了这样的代码:

int foo(boost::variant<int, std::string, Bar> const& v) {
    if (int const* i = get<int>(&v)) {
        return *i;
    }
    if (std::string const* s = get<std::string>(&v)) {
        return boost::lexical_cast<int>(*s);
    }
    if (Bar const* b = get<Bar>(&v)) {
        return b->toInt();
    }

    std::abort(); // ?
}
intfoo(boost::variant const&v){
if(int const*i=get&v)){
返回*i;
}
if(std::string const*s=get(&v)){
返回boost::词法转换(*s);
}
if(Bar const*b=获取(&v)){
返回b->toInt();
}
std::abort();/?
}
可以说,这是丑陋的。。。此外,还有一个问题,如果您突然向变体添加了一种类型,您需要检查代码中对它的每一次使用,以检查您是否在某个地方缺少
if

另一方面,如果您使用的是变体,如果您无法处理案例(类型),您将收到编译时错误的通知


在我看来,使用
boost::static\u visitor
是非常优越的。。。尽管我已经多次使用了
get()
备选方案;通常情况下,我只需要检查一种(或两种)类型,而不关心所有其他类型。另一种方法是使用带有
模板void操作符()的访问者(T const&)const重载,这不一定更干净。

您不必强制使用访问者,您可以使用
get()
完美地查询底层类型

这导致了这样的代码:

int foo(boost::variant<int, std::string, Bar> const& v) {
    if (int const* i = get<int>(&v)) {
        return *i;
    }
    if (std::string const* s = get<std::string>(&v)) {
        return boost::lexical_cast<int>(*s);
    }
    if (Bar const* b = get<Bar>(&v)) {
        return b->toInt();
    }

    std::abort(); // ?
}
intfoo(boost::variant const&v){
if(int const*i=get&v)){
返回*i;
}
if(std::string const*s=get(&v)){
返回boost::词法转换(*s);
}
if(Bar const*b=获取(&v)){
返回b->toInt();
}
std::abort();/?
}
可以说,这是丑陋的。。。此外,还有一个问题,如果您突然向变体添加了一种类型,您需要检查代码中对它的每一次使用,以检查您是否在某个地方缺少
if

另一方面,如果您使用的是变体,如果您无法处理案例(类型),您将收到编译时错误的通知


在我看来,使用
boost::static\u visitor
是非常优越的。。。尽管我已经多次使用了
get()
备选方案;通常情况下,我只需要检查一种(或两种)类型,而不关心所有其他类型。另一种方法是使用带有
模板void操作符()的访问者(T const&)const重载,这不一定更干净。

如果您希望对变量进行一些操作,例如一些检查,那么您可能希望将其作为访问者

struct to_str : boost::static_visitor<std::string>
{
   template<class T>
   std::string operator()(T const & x) const
   {
      return boost::lexical_cast<std::string>(x);
   }
};

如果您想对变量进行一些操作,例如一些检查,那么您可能希望将其作为访问者

struct to_str : boost::static_visitor<std::string>
{
   template<class T>
   std::string operator()(T const & x) const
   {
      return boost::lexical_cast<std::string>(x);
   }
};

我能不能不使用
SomeVariantVariable.which()
简单地查询底层类型,它将返回一个
typedef boost::variant variant_variable
声明的索引,并指示哪种类型:Type1=0,Type2=1?@Mushy:可以是,然后
切换该类型并调用
get()
;但是现在你只是在重新实现调用访问者的代码。。。除非有人在列表中间添加了一个类型,然后索引会移位,因此<代码> GET()>代码>可能返回NULL(或崩溃)。
它将返回一个
typedef boost::variant variant_variable
声明的索引,并指示哪种类型:Type1=0,Type2=1?@Mushy:您可以选择是,然后
切换该属性并调用
get()
;但是现在你只是在重新实现调用访问者的代码。。。除非有人在列表中间添加了一个类型,然后索引移动,因此<代码> GET()>代码>可能返回NULL(或崩溃)。