ebi*_*ebi 45 c++ c++11 list-initialization
std::vector<char> p = {"abc", "def"};
Run Code Online (Sandbox Code Playgroud)
"abc"并且"def"不是char,为什么编译器没有给我一个关于这种类型不匹配的错误?
Pra*_*ian 57
你不是在调用vector构造函数initializer_list<char>.那个构造函数不可行,因为正如你所说,你没有传递一个chars 列表.
但是vector也有一个构造函数,它将迭代器带到一系列元素中.
template< class InputIt >
vector( InputIt first, InputIt last,
const Allocator& alloc = Allocator() );
Run Code Online (Sandbox Code Playgroud)
不幸的是,这个构造函数匹配,因为两个参数将各自隐式转换为char const *.但是您的代码具有未定义的行为,因为传递给构造函数的开始和结束迭代器不是有效范围.
| 归档时间: |
|
| 查看次数: |
1656 次 |
| 最近记录: |