c ++ 11数组初始化不会调用副本构造函数 [英] c++11 array initialization won't call copy constructor

查看:130
本文介绍了c ++ 11数组初始化不会调用副本构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个小类,该类使用按其大小模板化的数组.这是一些代码...

I'm making a little class that uses an array templated on its size. Here's some code...

.hpp

template <size_t N>
class KeyCombinationListener
{
public:
    KeyCombinationListener(
        const std::array<sf::Keyboard::Key, N>& sequence,
        std::function<void (void)> fn
        );

private:
    std::array<sf::Keyboard::Key, N>  combo;
    std::function<void (void)>  callback;
};

.cc

template <size_t N>
KeyCombinationListener<N>::KeyCombinationListener(
    const array<sf::Keyboard::Key, N>& sequence, function<void (void)> fn
    ) : combo(sequence), progress{begin(combo)}, callback{fn}
{

}

在构造函数的成员初始化中,我不能将combo{sequence}用作初始化程序,因为它仅接受sf::Keyboard::Key类型.如果要输入initializer_list,这是有道理的,但这对我来说似乎很奇怪.使用其他标准容器,我可以使用{}表示法来调用复制构造函数.这是std::array的怪癖吗?还是我叮当中的错误?

In the member initialization of the constructor, I can't use combo{sequence} as the initializer because it only accepts sf::Keyboard::Key types. This makes sense if it's asking for an initializer_list, but this seems strange to me. With other standard containers I can call a copy constructor using {} notation just fine. Is this a quirk with std::array? Or maybe a bug in my clang?

以防万一,这是我的clang版本:

Just in case it helps, here's my clang version:

Debian clang version 3.5.0-10 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.2
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Candidate multilib: .;@m64
Selected multilib: .;@m64

推荐答案

您在C ++中遇到了一个缺陷:从单个元素进行列表初始化. C ++ 11和C ++ 14国际标准中指定的行为令人惊讶.我将在下面参考C ++ 14.

You've encountered a defect in C++: list-initialization from a single element. The behaviour specified in the C++11 and C++14 International Standard is surprising. I'll refer to C++14 below.

std::array的模板实例是聚合 [array.overview]/2 .因此,当从支撑初始化列表初始化std::array对象时,将不加选择地使用初始化器 [dcl.init]的数量来执行 aggregate-initialization .列表]/3.1 .由于某些构造的要求(例如,来自一对迭代器),其他容器类不能为集合.

Template instantiations of std::array are aggregates [array.overview]/2. Therefore, when initializing std::array objects from a braced-init-list, aggregate-initialization will be performed indiscriminately of the number of initializers [dcl.init.list]/3.1. Other container classes cannot be aggregates because of the requirements for certain constructions (e.g. from a pair of iterators).

聚集初始化初始化(可能递归地)来自初始化程序的数据成员.在您的情况下,它将尝试从初始化程序sequence(具有相同类型)初始化std::array<sf::Keyboard::Key, N>的第一个数据成员.对于std::array的所有实现,我都知道std::array的第一个数据成员是C样式数组.然后,列表初始化将尝试从原始初始化程序sequence初始化该数组的第一个元素.

Aggregate-initialization initializes (potentially recursively) the data members from the initializers. In your case, it will try to initialize the first data member of std::array<sf::Keyboard::Key, N> from the initializer sequence (which is of the same type). For all implementations of std::array I know, the first data member of std::array is a C-style array. List-initialization will then try to initialize the first element of that array from the original initializer: sequence.

示例:

struct aggregate
{
    int m[2];
};

aggregate x = {0, 1};
assert(x.m[0] == 0 && x.m[1] == 1);

aggregate y{x}; // error: cannot convert `aggregate` to `int`

最后一行的初始化将尝试从x初始化y.m[0].

The initialization in the last line will try to initialize y.m[0] from x.

CWG问题1467 对此进行了描述,一个相关的问题,没有初始化程序时进行列表初始化.拟议的决议为列表初始化引入了(还有另一种)特殊情况,涵盖了OP中的问题.引用最近的github草案[dcl.init.list]/3.1

CWG issue 1467 describes this and a related issue, list-initializing when there are no initializers. The proposed resolution introduces a (yet another) special case for list-initialization that covers the issue in the OP. Quoting a recent github draft, [dcl.init.list]/3.1

如果T是类类型,并且初始化列表具有单个元素 类型 cv U,其中UT或从T派生的类,对象是 从该元素初始化(通过复制初始化 复制列表初始化或通过直接初始化 直接列表初始化).

If T is a class type and the initializer list has a single element of type cv U, where U is T or a class derived from T, the object is initialized from that element (by copy-initialization for copy-list-initialization, or by direct-initialization for direct-list-initialization).

最近的草稿中的聚合初始化具有较低的优先级"(3.3),也就是说,只有在不满足上述条件的情况下才会执行.

Aggregate-initialization in recent drafts has lower "priority" (3.3), that is, will only be performed if the condition above is not met.

即使在C ++ 11模式下,最新版本的g ++(5.0)和clang ++(3.7.0)也会实现建议的分辨率.

Recent versions of g++ (5.0) and clang++ (3.7.0) implement the proposed resolution even in C++11 mode.

这篇关于c ++ 11数组初始化不会调用副本构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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