std::vector<bool>::resize 的最终默认参数是标准的吗?

Is the final default argument of std::vector<bool>::resize standard?

我的编译器 (MSVC2012) 对于

的最后一个参数默认为 false

std::vector<bool>::resize(std::vector<bool>::size_type, bool)

这是标准 C++ 还是 Microsoft 扩展?

我不认为非专业化 resize 是正确的,其中任何因大小增加而引入的元素都 初始化。

http://en.cppreference.com/w/cpp/container/vector_bool好像没说清楚

来自 C++ 标准

void resize(size_type sz, bool c = false);

至于一般class std::vector那么函数重载

void resize(size_type sz);
void resize(size_type sz, const T& c);

默认插入第一个函数元素。

我认为对于 std::vector<bool> 函数可以声明为

void resize(size_type sz, bool c = bool());

Is this standard C++ or a Microsoft extension?

是的,默认参数是标准的。 C++11 [vector.bool] 指定

void resize(size_type sz, bool c = false);

I don't think it's true for the unspecialised resize, where any elements introduced as a result of a size increase are not initialised.

是的;虽然自 C++11 以来,这是通过两个重载实现的,而不是默认参数,因此如果您不指定值,它们将被值初始化,而不是从值初始化的临时值中复制。

17.6.5.5 [member.functions] 说:

An implementation may declare additional non-virtual member function signatures within a class:
— by adding arguments with default values to a member function signature;187 [ Note: An implementation may not add arguments with default values to virtual, global, or non-member functions. — end note ]
— by replacing a member function signature with default values by two or more member function signatures with equivalent behavior; and
— by adding a member function signature for a member function name.
A call to a member function signature described in the C ++ standard library behaves as if the implementation declares no additional member function signatures.

这允许实现 add/remove 默认参数,因此完全符合替换这两个签名的要求:

void resize(size_type);
void resize(size_type, bool);

带有默认参数的单个函数:

void resize(size_type, bool = false);

对于非专用 vector<T>::resize() C++11 标准从一个函数(带有默认参数)更改为两个重载,因此调用 1-argument 形式不再需要类型可复制构造。实现不能使用默认参数,因为那会违反上面引用的最后一行,因为行为会有所不同。这与 vector<bool> 无关,因为我们总是知道 bool 是可复制构造的,因此实现可以选择是定义一个 vector<bool>::resize 成员函数还是两个。