为什么std::list中的单参数构造函数定义为显式
Why single argument constructors in std::list defined as explicit
我做了一些关于定义显式构造函数(link1, link2, link3, link4, link5)的研究。
但是对于我来说,std::list和std::iterator单参数构造函数被定义为显式的原因仍然不明显,在实际情况下,这个定义可能会有所帮助。你能举一些例子说明这个定义有助于避免bug吗?由于
explicit list(const _Alloc& _Al)
: _Mybase(_Al)
{ // construct empty list, allocator
}
explicit back_insert_iterator(_Container& _Cont)
: container(_STD addressof(_Cont))
{ // construct with container
}
下面的代码是好的和不言自明的吗?我们使用分配器复制初始化list,而人们期望插入list的value_type
元素。显式构造函数禁止这种滥用。
myallocator<int> allocator;
std::list<int, myallocator<int> > lst = allocator;
或
void f(const std::list<int, myallocator<int> >& lst)
{
}
myallocator<int> alloc;
f(alloc);
相关文章:
- 具有enable_if外部类原型的模板类构造函数定义
- 构造函数定义中类声明中的模板值
- C++中的继承和构造函数定义
- 如何使用模板化构造函数定义演绎指南?
- 构造函数定义C
- 不正确的成员构造函数定义
- 具有虚拟多重继承的构造函数定义
- C++谷歌测试在哪里放置测试夹具构造函数定义
- 模板类定义中的模板构造函数定义
- C++下面给出的代码中的构造函数定义差异
- 带继承的构造函数定义
- 无法访问构造函数定义之外的变量
- 调用构造函数定义中的函数后收到Segmentation Fault 11错误
- 将具有默认值的构造函数定义为组合对象的私有字段
- 防止重新定义构造函数定义的方法
- 继承构造函数-定义可移植的固定大小类型(C++)
- 在类的私有部分为复制构造函数定义原型如何防止类的复制
- 如何指定继承类的构造函数定义
- 了解默认移动构造函数定义
- 理解构造函数定义块的语法变化