std::vector<char> p = {"abc", "def"};
"abc"
和"def"
不是char
,为什么编译器没有给出关于这种类型不匹配的错误?
You're not calling vector
's constructor that takes an initializer_list<char>
. That constructor is not viable because, as you said, you're not passing a list of char
s.
But vector
also has a constructor that takes iterators to a range of elements.
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 *
. But your code has undefined behavior because the begin and end iterators being passed to the constructor are not a valid range.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.