如何模拟数组c初始化" INT ARR [] = {E1,E2,E3,...}"用的std ::阵列的行为呢? [英] How to emulate C array initialization "int arr[] = { e1, e2, e3, ... }" behaviour with std::array?
问题描述
(注:这个问题是关于不必指定元素的数量,仍然允许直接初始化嵌套类型)结果
<一href=\"http://stackoverflow.com/questions/6111565/now-that-we-have-stdarray-what-uses-are-left-for-c-style-arrays\">This问题讨论的使用留给C数组像 INT改编[20];
。在<一个href=\"http://stackoverflow.com/questions/6111565/now-that-we-have-stdarray-what-uses-are-left-for-c-style-arrays/6113104#6113104\">his回答,@詹姆斯甘孜显示C数组的最后据点之一,它独特的初始化特性:
(Note: This question is about not having to specify the number of elements and still allow nested types to be directly initialized.)
This question discusses the uses left for a C array like int arr[20];
. On his answer, @James Kanze shows one of the last strongholds of C arrays, it's unique initialization characteristics:
int arr[] = { 1, 3, 3, 7, 0, 4, 2, 0, 3, 1, 4, 1, 5, 9 };
我们不必以指定的元素数,万岁!现在遍历它与C ++ 11函数的std ::开始
和的std ::结束
从&LT;&迭代器GT;
(<一个href=\"http://stackoverflow.com/questions/5897319/how-to-use-stdsort-to-sort-a-array-in-c/5897358#5897358\">or你自己的变种的),你永远不需要甚至认为它的大小。
We don't have to specify the number of elements, hooray! Now iterate over it with the C++11 functions std::begin
and std::end
from <iterator>
(or your own variants) and you never need to even think of its size.
现在,是否有任何(可能TMP)的方式来实现与 STD同一::阵列
?宏允许使用使它看起来更好。 :)
Now, are there any (possibly TMP) ways to achieve the same with std::array
? Use of macros allowed to make it look nicer. :)
??? std_array = { "here", "be", "elements" };
修改:中间版本,从不同的答案编制,看起来是这样的:
Edit: Intermediate version, compiled from various answers, looks like this:
#include <array>
#include <utility>
template<class T, class... Tail, class Elem = typename std::decay<T>::type>
std::array<Elem,1+sizeof...(Tail)> make_array(T&& head, Tail&&... values)
{
return { std::forward<T>(head), std::forward<Tail>(values)... };
}
// in code
auto std_array = make_array(1,2,3,4,5);
和员工都挺酷C ++ 11的东西:
And employs all kind of cool C++11 stuff:
- 可变参数模板
-
的sizeof ...
- 右值引用
- 完美转发
-
的std ::阵列
,当然 - 统一初始化
- 统一初始化省略返回类型
- 类型推断(
汽车
)
- Variadic Templates
sizeof...
- rvalue references
- perfect forwarding
std::array
, of course- uniform initialization
- omitting the return type with uniform initialization
- type inference (
auto
)
和一个例子可以发现这里。
然而,如@Johannes在@ Xaade的回答评论指出的那样,你无法初始化嵌套类型具有这样的功能。例如:
However, as @Johannes points out in the comment on @Xaade's answer, you can't initialize nested types with such a function. Example:
struct A{ int a; int b; };
// C syntax
A arr[] = { {1,2}, {3,4} };
// using std::array
??? std_array = { {1,2}, {3,4} };
另外,初始化的数目限制为实现支持的函数和模板参数的数目。
Also, the number of initializers is limited to the number of function and template arguments supported by the implementation.
推荐答案
我能想到的最好的是:
template<class T, class... Tail>
auto make_array(T head, Tail... tail) -> std::array<T, 1 + sizeof...(Tail)>
{
std::array<T, 1 + sizeof...(Tail)> a = { head, tail ... };
return a;
}
auto a = make_array(1, 2, 3);
然而,这需要编译器进行NRVO,然后还要跳过返回值的副本(这也是合法的,但不是必需的)。在实践中,我希望任何C ++编译器,以便能够优化,这样,它的速度是直接初始化
However, this requires the compiler to do NRVO, and then also skip the copy of returned value (which is also legal but not required). In practice, I would expect any C++ compiler to be able to optimize that such that it's as fast as direct initialization.
这篇关于如何模拟数组c初始化&QUOT; INT ARR [] = {E1,E2,E3,...}&QUOT;用的std ::阵列的行为呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!