C++初始化列表

发布于 2024-11-29 23:43:01 字数 709 浏览 1 评论 0原文

我必须用 struct MHD_OptionItem 类型的元素填充 std::vector。 这个结构有这样的实现:

struct MHD_OptionItem
{
    enum MHD_OPTION option;
    intptr_t value;
    void *ptr_value;
 };

我已经尝试过这种方式:

 vector<struct MHD_OptionItem> iov;
 if(...)
     iov.push_back({ MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL });
 if(...)
     iov.push_back({ MHD_OPTION_CONNECTION_TIMEOUT, connectionTimeout });
 [....]

但是g ++编译器,正如预期的那样,对我说:

警告:扩展初始化列表仅适用于-std = c ++ 0x或-std = gnu ++ 0x

我知道我可以初始化一个临时结构体,然后将其传递给向量,但在我看来,这种方法效率低下,而且不太优雅。

我无法更改插入构造函数的结构,因为这不是我的代码,而是包含的库。

有没有一种优雅的方法可以在不使用 c++0x 语法的情况下做到这一点?

I have to fill a std::vector with elements of type struct MHD_OptionItem.
This struct have this implementation:

struct MHD_OptionItem
{
    enum MHD_OPTION option;
    intptr_t value;
    void *ptr_value;
 };

I have tried this way:

 vector<struct MHD_OptionItem> iov;
 if(...)
     iov.push_back({ MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL });
 if(...)
     iov.push_back({ MHD_OPTION_CONNECTION_TIMEOUT, connectionTimeout });
 [....]

but the g++ compiler, as expected, says to me:

warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x

I know that I can initialize a temporary struct and then pass it to the vector, but this method seems to me to be inefficient and not so elegant.

I can't change the struct inserting a constructor because this is not my code but a library included.

There is an elegant way to do this without using c++0x syntax?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

揽清风入怀 2024-12-06 23:43:01

假设您无法更改 struct 或您想将其保留为 POD:

void f()
{
    struct {
        MHD_OptionItem operator ()(enum MHD_OPTION opt, intptr_t val, void *ptr = 0) {
            MHD_OptionItem x = {opt, val, ptr};
            return x;
        }
    } gen;

     vector<struct MHD_OptionItem> iov;
     if(...)
         iov.push_back(gen(MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL));
     if(...)
         iov.push_back(gen(MHD_OPTION_CONNECTION_TIMEOUT, connectionTimeout));
     [....]
}

另一种解决方案:

    struct Gen : MHD_OptionItem {
        Gen(enum MHD_OPTION opt, intptr_t val, void *ptr = 0) {
            option = opt;
            value = val;
            ptr_value ptr;
        }
    };

     vector<struct MHD_OptionItem> iov;
     if(...)
         iov.push_back(Gen(MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL));
     if(...)
         iov.push_back(Gen(MHD_OPTION_CONNECTION_TIMEOUT, connectionTimeout));
     [....]

Assuming you cannot change the struct or you want to leave it a POD:

void f()
{
    struct {
        MHD_OptionItem operator ()(enum MHD_OPTION opt, intptr_t val, void *ptr = 0) {
            MHD_OptionItem x = {opt, val, ptr};
            return x;
        }
    } gen;

     vector<struct MHD_OptionItem> iov;
     if(...)
         iov.push_back(gen(MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL));
     if(...)
         iov.push_back(gen(MHD_OPTION_CONNECTION_TIMEOUT, connectionTimeout));
     [....]
}

Another solution:

    struct Gen : MHD_OptionItem {
        Gen(enum MHD_OPTION opt, intptr_t val, void *ptr = 0) {
            option = opt;
            value = val;
            ptr_value ptr;
        }
    };

     vector<struct MHD_OptionItem> iov;
     if(...)
         iov.push_back(Gen(MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL));
     if(...)
         iov.push_back(Gen(MHD_OPTION_CONNECTION_TIMEOUT, connectionTimeout));
     [....]
看春风乍起 2024-12-06 23:43:01

您必须为您的结构创建一个构造函数。

struct MHD_OptionItem
{
    MHD_OptionItem(enum MHD_OPTION _option, intptr_t _value, void *_ptr_value)
    : option(_option), value(_value), ptr_value(_ptr_value) {}
    enum MHD_OPTION option;
    intptr_t value;
    void *ptr_value;
};

然后你可以这样初始化:

ov.push_back(MHD_OptionItem(MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL));

You have to create a constructor for your struct.

struct MHD_OptionItem
{
    MHD_OptionItem(enum MHD_OPTION _option, intptr_t _value, void *_ptr_value)
    : option(_option), value(_value), ptr_value(_ptr_value) {}
    enum MHD_OPTION option;
    intptr_t value;
    void *ptr_value;
};

Then you can initialize this way:

ov.push_back(MHD_OptionItem(MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL));
撩动你心 2024-12-06 23:43:01

引入一个构造函数和复制构造函数,

struct MHD_OptionItem
{
  //...
  MHD_OptionItem(MHD_OPTION, intptr_t, void*);  // makes struct a non-POD
  MHD_OptionItem(const MHD_OptionItem&) // copy constructor
};

并使用它进行初始化怎么样,

iov.push_back(MHD_OptionItem(MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL));

是的,你正在引入一个临时的。但语法仍然很优雅

How about having a constructor and copy constructor introduced,

struct MHD_OptionItem
{
  //...
  MHD_OptionItem(MHD_OPTION, intptr_t, void*);  // makes struct a non-POD
  MHD_OptionItem(const MHD_OptionItem&) // copy constructor
};

And using it for initialization,

iov.push_back(MHD_OptionItem(MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL));

Yes, you are introducing a temporary. But still the syntax remains elegant

酒浓于脸红 2024-12-06 23:43:01

也许您可以提供一个包含所有 3 个参数的构造函数,然后按如下方式创建它:

iov.push_back(MHD_OptionItem( MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL));

May be you can provide a constructor taking all the 3 parameters and then create it as follows:

iov.push_back(MHD_OptionItem( MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文