简体   繁体   English

gcc 不是 clang 上的错误:无法从“转换”<brace-enclosed initializer list> ' 到 'std::vector&lt;&gt;</brace-enclosed>

[英]error on gcc not clang: cannot convert from '<brace-enclosed initializer list>' to 'std::vector<>

The following code produces an error only with gcc:以下代码仅对 gcc 产生错误:

could not convert '{{"John", 32}, {"Theo", 23}, {"Aun", 43}, {"Vivien", 67}}' from '<brace-enclosed initializer list>' to 'std::vector<Person>'
                                 {"Vivien", 67}};
#include <vector>

struct Person{
    //const char* name; // <-- Compiles in both gcc and clang
    char name[32];      // <-- Error in gcc only
    int age;
};

int main(){
    std::vector<Person> parr1 = {{"John", 32},
                                 {"Theo", 23},
                                 {"Aun", 43},
                                 {"Vivien", 67}};
    return 0;
}

( https://godbolt.org/z/1qEaaqcac ) https://godbolt.org/z/1qEaaqcac

What is the reason for this?这是什么原因? Is there a way to get the char array version to compile with gcc?有没有办法让 char 数组版本与 gcc 一起编译?

I'm testing with gcc 8.3 and latest clang.我正在使用 gcc 8.3 和最新的 clang 进行测试。

This is likely due to literal strings being const char* to char[] , and therefore without an implicit constructor, the correct aggregate initializer may not be known, and if you want a quick way to compile this with GCC, either do one of the following:这可能是由于文字字符串是const char*char[] ,因此如果没有隐式构造函数,则可能不知道正确的聚合初始值设定项,如果您想要一种使用 GCC 快速编译它的方法,请执行以下操作之一下列的:

std::vector<Person> parr1 = {
    Person {"John", 32},
    Person {"Theo", 23},
    Person {"Aun", 43},
    Person {"Vivien", 67}
};

This first answer uses explicit aggregate initialization to ensure the type is known at compile time.第一个答案使用显式聚合初始化来确保在编译时知道类型。 Another option is:另一种选择是:

struct Person{
    const char* name;
    int age;
};

int main(){
    std::vector<Person> parr1 = {
        { "John", 32 },
        { "Theo", 23 },
        { "Aun", 43 },
        { "Vivien", 6 }
    };
    return 0;
}

This simply ensures there is no type conversions required to due aggregate initialization.这只是确保由于聚合初始化不需要类型转换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从大括号括起来的初始化列表转换为std :: vector - Could not convert from brace-enclosed initializer list to std::vector std::forward 无法转换大括号括起来的初始化列表 - std::forward cannot convert brace-enclosed initializer list 错误:无法转换&#39; <brace-enclosed initializer list> ()&#39;从&#39; <brace-enclosed initializer list> &#39;到&#39;结构&#39; - Error: could not convert '<brace-enclosed initializer list>()' from '<brace-enclosed initializer list>' to 'struct' 无法从大括号括起来的初始化列表转换为std元组 - Could not convert from brace-enclosed initializer list to std tuple 无法从 '<brace-enclosed initializer list> ' 为向量</brace-enclosed> - Could not convert from ‘<brace-enclosed initializer list>’ to vector 无法转换Brace封闭的初始化列表 - Cannot convert Brace-enclosed initializer list 无法从大括号括起初始化列表转换为struct - Cannot convert to struct from brace-enclosed initializer list 无法转换&#39;( <expression error> )”,来自“ <brace-enclosed initializer list> &#39;到std :: unique_ptr <int []> - could not convert '(<expression error>)' from '<brace-enclosed initializer list>' to std::unique_ptr<int []> 错误:无法从 '<brace-enclosed initializer list> ' 到 'std::tuple<void (xxx::*)(), xxx> '</void></brace-enclosed> - error: could not convert from '<brace-enclosed initializer list>' to 'std::tuple<void (xxx::*)(), xxx>' 错误:没有匹配的 function 调用 'std::vector<pet*> ::向量(<brace-enclosed initializer list> )'</brace-enclosed></pet*> - error: no matching function for call to 'std::vector<Pet*>::vector(<brace-enclosed initializer list>)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM