【发布时间】:2019-12-16 21:42:30
【问题描述】:
std::vector<bool>::emplace_back 是 available 仅从 C++14 开始。但是,使用我的 g++ 5.4.0,即使我指定 -std=c++11,它也可以编译。
当我用 g++ 4.8.4 编译相同的代码时,它失败了。有没有办法说服 g++ 严格检查所选标准?
注意:我已经使用-pedantic -Wextra -Wall。
例如test.cpp:
#include <vector>
int main()
{
std::vector<bool> v;
v.emplace_back(true);
return 0;
}
使用g++ -std=c++11 test.cpp -o test 编译。使用 g++ 5.4.0 编译正常,g++ 4.8.4 触发:
test.cpp: In function ‘int main()’:
test.cpp:6:7: error: ‘class std::vector<bool>’ has no member named ‘emplace_back’
v.emplace_back(true);
^
【问题讨论】:
-
缺少
vector<bool>::emplace_back并不是对符合实现的约束。它是对符合 programs 的约束。一个实现可以在它认为合适的时候在标准类中定义额外的成员。 -
相关(相同观察):std::vector<bool>::emplace in C++11
-
@n.m.标准在哪里说实现可能包括其他成员?
-
@hyde: Demo 带有 OP 的代码(编辑后)。
-
@Ayxan 在 C++11 Stadnard 中,它也在脚注 188 中:实现还可以定义其他成员函数,否则这些成员函数不会被有效的 C++ 程序调用。我>