gpt4 book ai didi

c++ - 是否有可能在没有得到 "new"ed 的结构中有一个 boost::variant?

转载 作者:行者123 更新时间:2023-11-28 03:19:13 24 4
gpt4 key购买 nike

我已经创建了结构

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

是否可以像这样创建事件:

Event e;

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

Event e = new Event();

*编辑:* 这是我收到的错误:错误 C2061:语法错误:标识符“storage_”(在 variant.hpp 中)

variant.hpp 中有一些注释,但我无法理解它们,因为“第一个绑定(bind)类型是 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 的情况下创建它。如果您需要进一步的帮助,我建议您展示演示问题的完整代码以及编译器错误消息。

关于c++ - 是否有可能在没有得到 "new"ed 的结构中有一个 boost::variant?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15962014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com