码:
std::vector<int> x{1,2,3,4};
std::array<int, 4> y{{1,2,3,4}};
Run Code Online (Sandbox Code Playgroud)
为什么我需要std :: array的双花括号?
编译下面的代码时,我在VC2010中遇到错误C2078.
struct A
{
int foo;
double bar;
};
std::array<A, 2> a1 =
// error C2078: too many initializers
{
{0, 0.1},
{2, 3.4}
};
// OK
std::array<double, 2> a2 = {0.1, 2.3};
Run Code Online (Sandbox Code Playgroud)
我发现正确的语法a1是
std::array<A, 2> a1 =
{{
{0, 0.1},
{2, 3.4}
}};
Run Code Online (Sandbox Code Playgroud)
问题是:为什么需要额外的括号a1但不是必需的a2?
更新
这个问题似乎并不特定于std :: array.一些例子:
struct B
{
int foo[2];
};
// OK
B meow1 = {1,2};
B bark1 = {{1,2}};
struct C
{
struct
{
int a, b;
} …Run Code Online (Sandbox Code Playgroud) 为什么这样做:
std::pair<int, int> p = {1,2};
std::vector<std::pair<int, int>> vp = { {1,2}, {3,4} };
Run Code Online (Sandbox Code Playgroud)
但这不是吗?
std::array<int, 2> a = {1,2}; // still ok
std::vector<std::array<int, 2>> va = { {1,2}, {3,4} };
Run Code Online (Sandbox Code Playgroud)
使用g ++ 4.5.1 -std=c++0x,第二行失败:
错误:无法转换
‘{{1, 2}, {3, 4}}’为‘std::vector<std::array<int, 2u> >’
谢谢
请考虑以下代码:
#include <array>
struct A
{
int a;
int b;
};
static std::array<A, 4> x1 =
{
{ 1, 2 },
{ 3, 4 },
{ 5, 6 },
{ 7, 8 }
};
static std::array<A, 4> x2 =
{
{
{ 1, 2 },
{ 3, 4 },
{ 5, 6 },
{ 7, 8 }
}
};
static std::array<A, 4> x3 =
{
A{ 1, 2 },
A{ 3, 4 },
A{ 5, 6 },
A{ 7, 8 …Run Code Online (Sandbox Code Playgroud) A std::array<T>本质上是一个包裹在一个C风格的数组struct.structs 的初始化需要大括号,并且数组的初始化也需要大括号.所以我需要两对括号:
std::array<int, 5> a = {{1, 2, 3, 4, 5}};
Run Code Online (Sandbox Code Playgroud)
但是我见过的大多数示例代码只使用了一对大括号:
std::array<int, 5> b = {1, 2, 3, 4, 5};
Run Code Online (Sandbox Code Playgroud)
为什么这是允许的,与第一个approch相比,它有任何好处或缺点吗?
对不起,如果标题令人困惑,我找不到一个简单的方法来写一个简单的句子.无论如何,我面临的问题是:
// header:
class SomeThing
{
private:
SomeThing() {} // <- so users of this class can't come up
// with non-initialized instances, but
// but the implementation can.
int some_data; // <- a few bytes of memory, the default
// constructor SomeThing() doesn't initialize it
public:
SomeThing(blablabla ctor arguments);
static SomeThing getThatThing(blablabla arguments);
static void generateLookupTables();
private:
// declarations of lookup tables
static std::array<SomeThing, 64> lookup_table_0;
static SomeThing lookup_table_1[64];
};
Run Code Online (Sandbox Code Playgroud)
该getThatThing函数用于从查找表返回实例.
// in the implementation file - …Run Code Online (Sandbox Code Playgroud) 在以下代码中:
template<size_t N>
int b(int q, const std::array<int, N>& types)
{
int r = q;
for (int t : types)
{
r = r + t;
}
return r;
}
int main()
{
b<2>(9, { 2,3 });
}
Run Code Online (Sandbox Code Playgroud)
如何避免在调用 b 时为 N 指定 2?为什么不能自动推导出这种类型?没有它我得到错误:
“b”:找不到匹配的重载函数“int b(int,const std::array &)”:无法推导出“N”的模板参数
为什么我不能像这样对std :: array进行脱盐?
#include <array>
struct Point
{
float x;
float y;
};
int main()
{
std::array<Point, 3> m_points {
{ 1.0f, 1.0f },
{ 2.0f, 2.0f },
{ 3.0f, 3.0f }
};
}
Run Code Online (Sandbox Code Playgroud)
这样做我得到错误:
错误:初始化程序太多了
std::array<Point, 3ul>
但它的工作原理如下:
std::array<Point, 3> m_points {
Point{ 1.0f, 1.0f },
Point{ 2.0f, 2.0f },
Point{ 3.0f, 3.0f }
};
Run Code Online (Sandbox Code Playgroud)
相比之下,std::map可以使用以下两种方式初始化:
std::map<int, int> m1 {std::pair<int, int>{1,2}, std::pair<int, int>{3,4}};
std::map<int, int> m2 {{1,2}, {3,4}};
Run Code Online (Sandbox Code Playgroud) 如果我std::array<T, N>用大括号构造 a 并给它少于N项目,那么这些项目是否为零初始化?(或者它们是否保留默认初始化?)如果我给它零个项目(即= {}),那么我相信它会将所有元素零初始化。
我找不到这个简单问题的明确答案。由于std::array在像 一样使用时使用聚合初始化std::array<int, 2> x = { 1 };,因此导致聚合初始化规则的https://en.cppreference.com/w/cpp/language/aggregate_initialization 。在那里,我看到的关于这种情况的唯一提及是“如果指定了数组的大小并且它大于字符串文字中的字符数,则其余字符将被零初始化。” 但这是在“字符数组”部分中,所以看起来通常不是这样。另一方面,用作constexrpUB 检测器表明它们已归零: https: //godbolt.org/z/zE9xKvbrq
有关的:
c++ ×9
c++11 ×6
stdarray ×3
arrays ×2
c++14 ×2
aggregate ×1
curly-braces ×1
declaration ×1
stl ×1