是否可以在结构中有一个没有被"new"编辑的 boost::variant ?
Is it possible to have a boost::variant inside a struct that does not get "new"ed?
我已经创建了结构体
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)。如果不希望这样做,或者如果第一个有界类型不是默认可构造的,则可以从可转换为其有界类型之一的任何值直接构造变体"
相关文章:
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 如果"new int"返回"int*",那么为什么"new int[n]"不返回"int**"?
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 重载运算符new[]的行为取决于析构函数
- 编辑文件中的文本
- 过载'operator new'如何导致无限循环?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- Codelite C++ new project
- 在类c++中使用new声明数组
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- g++用户定义的动态链接库上的全局new和delete运算符
- SetDlgItemInt 不会更改嵌入资源的编辑框
- 如果整个应用程序是虚拟映射的,为什么 new 会进行系统调用?
- 在将 new 与指针一起使用时,创建数组的指定长度
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- C++,Qt:仅在双击后编辑qtablewidget项目
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- 编辑字符串以包含括号 c++ 之间的变量
- Arduino函数无法编辑数组
- 是否可以在结构中有一个没有被"new"编辑的 boost::variant ?