I did some research about defining explicit constructors ( link1 , link2 , link3 , link4 , link5 ).
But for me it is still not obvious why std::list and std::iterator single argument constructors defined as explicit and in which practical case this definition may be helpful. Can you please bring some examples where this definition helps to avoid bugs. Thanks
explicit list(const _Alloc& _Al)
: _Mybase(_Al)
{ // construct empty list, allocator
}
explicit back_insert_iterator(_Container& _Cont)
: container(_STD addressof(_Cont))
{ // construct with container
}
Is the following code good and self-explanatory? We copy-initialize list with an allocator, while people expect list's value_type
elements to be inserted. Explicit constructors forbid such misuses.
myallocator<int> allocator;
std::list<int, myallocator<int> > lst = allocator;
or
void f(const std::list<int, myallocator<int> >& lst)
{
}
myallocator<int> alloc;
f(alloc);
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.