如何正确使用大括号包围的初始化器?

huangapple go评论75阅读模式
英文:

How to properly use brace enclosed initiator?

问题

I have defined 3 nested structs in my project and want to create a default setup that is used by many functions. I have tried using the following code which the IDE says should work.

struct xBitValue {
    size_t depth=0;
    uint64_t key=1;

    //allow comparison
    bool operator==(const xBitValue& b) const {
        return ((depth==b.depth) && (key==b.key));
    }
    bool operator!=(const xBitValue& b) const {
        return ((depth!=b.depth) || (key!=b.key));
    }
};

struct stringHeaderOption {
    xBitValue header;
    size_t encoder=0;
};

struct stringHeaderOptions {
    size_t xBitLength;
    std::vector<stringHeaderOption> options;
};

const stringHeaderOptions defaultHeaderOptions={2,{
    {{0,1},0},
    {{0,2},1},
    {{0,3},2},
    {{1,1},3}
}};

but I get the error could not convert {{{0, 1}, 0}, {{0, 2}, 1}, {{0, 3}, 2}, {{1, 1}, 3}} from <brace-enclosed initializer list> to std::vector<stringHeaderOption> with an error pointing to the last }.

It seems there is something I am missing on how to initialize values in this way.

C++11

英文:

I have defined 3 nested structs in my project and want to create a default setup that is used by many functions. I have tried using the following code which the IDE says should work.

struct xBitValue {
    size_t depth=0;
    uint64_t key=1;

    //allow comparison
    bool operator==(const xBitValue&amp; b) const {
        return ((depth==b.depth) &amp;&amp; (key==b.key));
    }
    bool operator!=(const xBitValue&amp; b) const {
        return ((depth!=b.depth) || (key!=b.key));
    }
};

struct stringHeaderOption {
    xBitValue header;
    size_t encoder=0;
};

struct stringHeaderOptions {
    size_t xBitLength;
    std::vector&lt;stringHeaderOption&gt; options;
};

const stringHeaderOptions defaultHeaderOptions={2,{
    {{0,1},0},
    {{0,2},1},
    {{0,3},2},
    {{1,1},3}
}};

but I get the error could not convert {{{0, 1}, 0}, {{0, 2}, 1}, {{0, 3}, 2}, {{1, 1}, 3}} from &lt;brace-enclosed initializer list&gt; to std::vector&lt;stringHeaderOption&gt; with an error pointing to the last }.

It seems there is something I am missing on how to initialize values in this way.

C++11

答案1

得分: 3

你的目标是进行聚合初始化。然而,在C++11中,聚合类型不能具有默认成员初始化器 - 这在C++14中已更改(参见:https://en.cppreference.com/w/cpp/language/aggregate_initialization#Definitions)

你的代码在C++14及更高版本中编译通过。在C++11中,它将无法工作;

在C++11中,如果删除对xBitValue和stringHeaderOption的数据成员提供的默认值,它将编译通过。
请参见:http://coliru.stacked-crooked.com/a/eb49bcb837a45f89

**注意:**在C++14及更高版本中,以下代码可以编译通过(在C++11中将导致错误):

stringHeaderOption s1{1,0,1}; 
stringHeaderOption s2{1,0};
stringHeaderOption s3{1};

因此,也许你只想允许创建你认为合法的结构的构造函数。

英文:

You are aiming for an aggregate initialization.
However, In C++11 an aggregate cannot have default member initializers - this was changed in C++14 (See: https://en.cppreference.com/w/cpp/language/aggregate_initialization#Definitions)

Your code compiles in C++14 and later. It will not work in C++11;
See it compile in C++14: http://coliru.stacked-crooked.com/a/8484258abc2e02d6

It will compile in C++11 if you remove the default values you are giving to the data members of xBitValue and stringHeaderOption.
See: http://coliru.stacked-crooked.com/a/eb49bcb837a45f89

Note: In C++14 and later the following compiles (in C++11 it will result in an error):

stringHeaderOption s1{1,0,1}; 
stringHeaderOption s2{1,0};
stringHeaderOption s3{1};

Therefore, perhaps you would like to only allow constructors that create structs you find legal.

huangapple
  • 本文由 发表于 2023年5月22日 12:09:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定