[英]Why does this code compile (C++11) without a type mismatch error?
std::vector<char> p = {"abc", "def"};
"abc"
和"def"
不是char
,为什么编译器没有给出关于这种类型不匹配的错误?
You're not calling vector
's constructor that takes an initializer_list<char>
. 你没有调用带有initializer_list<char>
vector
的构造initializer_list<char>
。 That constructor is not viable because, as you said, you're not passing a list of char
s. 那个构造函数不可行,因为正如你所说,你没有传递一个char
列表。
But vector
also has a constructor that takes iterators to a range of elements. 但是vector
也有一个构造函数 ,它将迭代器带到一系列元素中。
template< class InputIt >
vector( InputIt first, InputIt last,
const Allocator& alloc = Allocator() );
Unfortunately, this constructor matches because the two arguments will each implicitly convert to char const *
. 不幸的是,这个构造函数匹配,因为两个参数将隐式转换为char const *
。 But your code has undefined behavior because the begin and end iterators being passed to the constructor are not a valid range. 但是您的代码具有未定义的行为,因为传递给构造函数的开始和结束迭代器不是有效范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.