今天,我偶然发现这些标准声明 http://en.cppreference.com/w/cpp/container/vector/vector of std::vector
构造函数:
// until C++14
explicit vector( const Allocator& alloc = Allocator() );
// since C++14
vector() : vector( Allocator() ) {}
explicit vector( const Allocator& alloc );
这种变化可以在大多数标准容器中看到。一个稍微不同的例子是std::set
:
// until C++14
explicit set( const Compare& comp = Compare(),
const Allocator& alloc = Allocator() );
// since C++14
set() : set( Compare() ) {}
explicit set( const Compare& comp,
const Allocator& alloc = Allocator() );
这两种模式有什么区别,它们有什么(缺点?
它们是否严格等效 - 编译器是否生成与第一个类似的第二个?
不同之处在于
explicit vector( const Allocator& alloc = Allocator() );
is explicit
即使对于使用默认参数的情况,同时
vector() : vector( Allocator() ) {}
不是。 (这explicit
在第一种情况下有必要防止Allocator
s 从隐式转换为 avector
.)
这意味着你可以写
std::vector<int> f() { return {}; }
or
std::vector<int> vec = {};
属于第二种情况,但不是第一种情况。
See LWG 问题 2193 http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2193.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)