我应该如何支持初始化一个std ::数组的std ::对? [英] How should I brace-initialize an std::array of std::pairs?

查看:260
本文介绍了我应该如何支持初始化一个std ::数组的std ::对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std::array<std::pair<int, int>, 2> ids = { { 0, 1 }, { 1, 2 } };

VS2013错误:


错误C2440:'initializing':无法从'int'转换为'std :: pair'没有构造函数可以接受源类型,或者构造函数重载解析是不明确的

error C2440: 'initializing' : cannot convert from 'int' to 'std::pair' No constructor could take the source type, or constructor overload resolution was ambiguous`

我做错了什么?

推荐答案

添加另一对大括号。 / p>

Add another pair of braces.

std::array<std::pair<int, int>, 2> ids = { { { 0, 1 }, { 1, 2 } } };

std :: array< T,N> 是包含类型 T [N] 的成员的聚合类。通常,你可以用一个简单的 T [N] 数组来初始化,但是当你处理非聚合元素类型时,你可能需要更明确。

std::array<T, N> is an aggregate class containing a member of type T[N]. Usually, you can initialise that the same way you would a plain T[N] array, but when you're dealing with a non-aggregate element type, you may need to be more explicit.

这篇关于我应该如何支持初始化一个std ::数组的std ::对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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