![](/img/trans.png)
[英]Why does GCC 6.3 compile this Braced-Init-List code without explicit C++11 support?
[英]Why does this code compile (C++11) without a type mismatch error?
std::vector<char> p = {"abc", "def"};
"abc"
和"def"
不是char
,为什么编译器没有给出关于这种类型不匹配的错误?
你没有调用带有initializer_list<char>
vector
的构造initializer_list<char>
。 那个构造函数不可行,因为正如你所说,你没有传递一个char
列表。
但是vector
也有一个构造函数 ,它将迭代器带到一系列元素中。
template< class InputIt >
vector( InputIt first, InputIt last,
const Allocator& alloc = Allocator() );
不幸的是,这个构造函数匹配,因为两个参数将隐式转换为char const *
。 但是您的代码具有未定义的行为,因为传递给构造函数的开始和结束迭代器不是有效范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.