根据cppreference.com
Notes 专门化
std::vector<bool>直到C++14才有emplace()成员。
什么意思?模板专门化可以省略成员吗?是否意味着成员函数根本不存在?还是它不是专门的?
下面的代码似乎适用于gcc 7.1.0:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
std::vector<bool> v;
v.emplace_back(true);
v.emplace_back(false);
for (const auto &b : v) {
std::cout << b << std::endl;
}
}http://coliru.stacked-crooked.com/a/07c8c0fc6050968f
发布于 2017-07-07 13:04:54
模板专门化可以与一般模板完全不同。因此,在理论上,您无法知道类A<int>和A<bool>是否有任何公共成员:
template <typename T>
class A
{
public:
void foo()
{}
};
template <>
class A<bool>
{
public:
void bar()
{}
};
int main()
{
A<int> a;
a.foo();
A<bool> b;
b.bar();
b.foo(); // error: 'class A<bool>' has no member named 'foo'
}类std::vector<bool>是一个专门的模板。您从标准中引用的话说,emplace()方法在该专门化过程中一直缺失,直到C++14为止。
在实践中,诸如libstdc++这样的具体标准库可能在C++14之前就已经为std::vector<bool>提供了emplace()方法,但在标准中并不是必需的。
https://stackoverflow.com/questions/44971360
复制相似问题