检查的boost ::变体LT; T>对于空 [英] check boost::variant<T> for null

查看:154
本文介绍了检查的boost ::变体LT; T>对于空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的节目一个boost ::变种,我要检查,如果变量本身初始化,还是否有包含在它的一个值的类型。

I have a boost::variant in my program and I want to check if the variant itself is initialized and also if there is a value contained in one of it's types.

我就试过变种空(),但似乎并没有工作。无论是不检查对NULL。

I've tried empty() on the variant, but that doesn't seem to work. Neither does checking against NULL.

有谁知道如何检查呢?

编辑:好吧,看来它永远不会是空的,但不会永远是它包含类型的值,所以我怎么检查一个没有价值的情况

Ok, It seems it will never be empty, but there will not always be a value in it's contained types, so how do I check for a no-value situation?

推荐答案

A 的boost ::变种总是被初始化。

如果你没有明确initalized它,第一个项目是使用它的默认构造函数构造:

If you did not initalized it explicitly, the first item was constructed using its default constructor:

struct Foo {};
struct Bar {};

struct Visitor: boost::static_visitor<>
{
  void operator()(Foo const& foo) const { std::cout << "Foo\n"; }
  void operator()(Bar const& bar) const { std::cout << "Bar\n"; }
};

int main(int argc, char* argv[])
{
  boost::variant<Foo,Bar> var;
  boost::apply_visitor(Visitor(), var); // prints Foo
  return 0;
}

这篇关于检查的boost ::变体LT; T&GT;对于空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆