是否可以在结构中有一个没有被"new"编辑的 boost::variant ?



我已经创建了结构体

struct Event
{
  int key;
  boost::variant<int, float> value;
};

是否可以这样创建事件:

Event e;

我已经尝试过了,但我得到编译器错误。这是可能的还是我必须这样做:

Event e = new Event();

*EDIT: *这是我得到的错误:错误C2061:语法错误:标识符'storage_' (in variant.hpp)

在variant.hpp中有一些注释,但我不能理解它们,因为"第一绑定类型是int??"

    // NOTE TO USER :
    // Compile error from here indicates that the first bound
    // type is not default-constructible, and so variant cannot
    // support its own default-construction.
    //

我的以下编译器(VS2012):

#include "boost/variant.hpp"
struct Event
{
  int key;
  boost::variant<int, float> value;
};
int main()
{
   Event e;
   return 0;
}

所以,是的,没有new也可以创建它。如果您需要进一步的帮助,我建议显示完整的代码,演示问题,以及编译器错误消息。

是有可能的。文档(http://www.boost.org/doc/libs/1_53_0/doc/html/variant/tutorial.html)中的一个示例是:

boost::variant< int, std::string > v;

默认情况下,一个变量默认构造它的第一个有界类型,所以v最初包含int(0)。如果不希望这样做,或者如果第一个有界类型不是默认可构造的,则可以从可转换为其有界类型之一的任何值直接构造变体"

相关内容

最新更新