C ++ 11中的std :: vector< bool> :: emplace [英] std::vector<bool>::emplace in C++11

查看:88
本文介绍了C ++ 11中的std :: vector< bool> :: emplace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 cppreference.com


注释

专业化 std: :vector< bool> 在C ++ 14之前没有 emplace()成员

The specialization std::vector<bool> did not have emplace() member until C++14.

是什么意思?模板专业化可以省略成员吗?是否意味着根本不存在成员功能?还是只是不专业?

What does it mean? Can template specialization omit member? Does mean that member function is not present at all? Or is it just not specialized?

以下代码似乎适用于gcc 7.1.0:

Following code seems to work on 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

推荐答案

模板专业化可以与通用模板完全不同。因此,从理论上讲,您不知道类 A< int> A< bool> 是否具有任何公共成员:

A template specialization can be completely different from the generic template. So in theory, you cannot know whether the classes A<int> and A<bool> have any common members:

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> 是一个专门的模板。您在标准中的引用表明,在C ++ 14之前,该专业化中缺少 emplace()方法。

The class std::vector<bool> is a specialized template. Your quote from the standard says that the emplace() method was missing in that specialization until C++14.

实际上,像libstdc ++这样的具体标准库可能已经为 std :: vector< bool> emplace()方法。 c>甚至在C ++ 14之前,但在标准中并不是必需的。

In practice, concrete standard libraries such as libstdc++ may have provided the emplace() method for std::vector<bool> even before C++14, but it was not required in the standard.

这篇关于C ++ 11中的std :: vector&lt; bool&gt; :: emplace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆