为什么std :: list中的单个参数构造函数定义为显式

T M*_*T M 3 c++ std c++11

我做了一些关于定义显式构造函数的研究(link1,link2,link3,link4,link5).

但对我来说,为什么std :: list和std :: iterator单个参数构造函数定义为显式并且在实际情况下这个定义可能有用的原因仍然不明显.能否请一些例子来说明这个定义有助于避免错误.谢谢

explicit list(const _Alloc& _Al)
    : _Mybase(_Al)
    {   // construct empty list, allocator
    }

explicit back_insert_iterator(_Container& _Cont)
    : container(_STD addressof(_Cont))
    {   // construct with container
    }
Run Code Online (Sandbox Code Playgroud)

For*_*veR 9

以下代码是否良好且不言自明?我们使用分配器复制初始化列表,而人们期望value_type插入列表的元素.显式构造函数禁止此类滥用.

myallocator<int> allocator;
std::list<int, myallocator<int> > lst = allocator;
Run Code Online (Sandbox Code Playgroud)

要么

void f(const std::list<int, myallocator<int> >& lst)
{
}

myallocator<int> alloc;
f(alloc);
Run Code Online (Sandbox Code Playgroud)