错误C3074:数组只能使用一个初始化,初始化列表 [英] error C3074: an array can only be initialized with an initializer-list

查看:2210
本文介绍了错误C3074:数组只能使用一个初始化,初始化列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的吊舱,补充交易和复合类型的数组类似一个小(pretty)打印机。虽然这样做,我也与初始化列表摆弄并在下面的声明

I am working on a petty (pretty) printer for PODs, STLs and Composite Types like Arrays. While doing so, I was also fiddling with Initialization Lists and came across the following declaration

std::vector<double[3]> arr{ { 10, 11, 12 }, { 20, 21, 22 } }; 

看起来既VC2013和g ++ 4.8是不是很高兴,并发出一致的错误信息,在任何情况下是不是很帮助我

It seems both VC2013 and G++ 4.8 is not quite happy and issues a consistent error message which in either case is not quite helpful to me

对于VC ++: 错误C3074:数组只能使用一个初始化列表初始化

对于G ++ 4.8: 错误:数组必须用括号括起来的初始化初始化

因此​​,无论初始化列表在这里不能使用,或者我的语法非常不好吧?

So either Initialization lists cannot be used here or my syntax is quite not right?

在类似的方面,以下语法似乎是有效

On a similar front, the following syntax seems to be valid

std::vector<std::array<int, 3>>  arr{ { 10, 11, 12 }, { 20, 21, 22 } };

什么是可能出现的问题与我的初始化列表?

What is the possible problem with my initialization list?


  • 注意我明白我应该使用的std ::阵列而不是C类型数组,但我只是尝试。

  • 注意如果您wan't玩弄这一点,这里有一个 IDEONE 版本

  • 注意同时,这将是非常有益的,如果你可以参考我回标准。

  • Note I understand I should use std::array instead of C type arrays but I am just experimenting.
  • Note If you wan't to play around with this, here is an IDEONE version
  • Note Also, it would be quite beneficial if you can refer me back to the standard.

推荐答案

阅读当前的C ++标准 1Y草案。

从表之前99:

T
  是
  EmplaceConstructible
  成
  X
  从
  ARGS
  ,零个或多个参数
  ARGS
  ,意味着
  下面前pression是良好形成的:
  allocator_traits ::结构(M,P,参数)

T is EmplaceConstructible into X from args , for zero or more arguments args , means that the following expression is well-formed: allocator_traits::construct(m, p, args)

表100:

X(il);              |  Equivalent to      | X(il.begin(), il.end());
--------------------+---------------------+--------------------------------
X(i, j);            |                     | Requires:
X a(i, j);          |                     | T shall be EmplaceConstructible
                                          | into X from *i.

所以的std ::矢量&lt;双[3]&GT; V {{1,2,3},{4,5,6}}; 是有效的当且仅当双击[3] EmplaceConstructible {1,2,3} 作为初始化列表中的一个元素被传递到 STD ::矢量&lt;双[3]方式&gt;

So std::vector<double[3]> v{ {1,2,3}, {4,5,6} }; is valid iff double[3] is EmplaceConstructible from {1,2,3} as an element of an initializer list being passed to a std::vector<double[3]>.

有一个关于前向迭代器子句为好,但是这是没有问题的(如的std :: initialzier_list 迭代器是前向迭代器)。

There is a clause about forward iterators as well, but that is no problem (as std::initialzier_list iterators are forward iterators).

的std ::矢量&lt; T&GT; 需要一个的std :: initializer_list&LT; T&GT; 参数

所以的std :: initializer_list&LT;双[3]方式&gt; 是候选名单

首先,的std :: initializer_list&LT;双[3]&GT; X = {{1.0,2.0,3.0}}; 未能在GCC编译器。但是假设是海湾合作委员会中的错误。

First, std::initializer_list<double[3]> x = {{1.0, 2.0, 3.0}}; fails to compile in gcc. But suppose that is a bug in gcc.

二, ::新(nullptr)双[3](的std :: initializer_list&LT;双&GT; {1.0,2.0,3.0}); 放置新的,而 EmplaceConstructable 在缺乏合适的构建覆盖的减少了,无法编译。

Second, ::new (nullptr) double[3](std::initializer_list<double>{1.0, 2.0, 3.0}); placement new, which EmplaceConstructable reduces to in the lack of a suitable construct override, fails to compile.

所以双击[3] 不是 EmplaceConstruble 的std :: initalizer_list&LT;双&GT; ,也没有从双击[3] ,也没有别的(如因为我用的不是因为什么在一个支架上,出现错误括号,在新的位置),除非确实分配器魔术我不知道,以避免放置新的。

So double[3] is not EmplaceConstruble from a std::initalizer_list<double> nor from a double[3] nor anything else (as the error occurs because I used a bracket, not because of what was in the brackets, in the placement new), unless the allocator does magic I am not aware of to avoid the placement new.

因此​​,你的code违反现行标准草案,大概C ++ 11,当然C ++ 03(这对集装箱严格的要求)。

Thus your code violates the current draft standard, and probably C++11, and certainly C++03 (which had stricter requirements on containers).

这篇关于错误C3074:数组只能使用一个初始化,初始化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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