stdarray

Array declaration and initialization in C++11

◇◆丶佛笑我妖孽 提交于 2019-11-30 05:56:56
问题 Here are 8 ways to declare and initialize arrays in C++11 that seems ok under g++ : /*0*/ std::array<int, 3> arr0({1, 2, 3}); /*1*/ std::array<int, 3> arr1({{1, 2, 3}}); /*2*/ std::array<int, 3> arr2{1, 2, 3}; /*3*/ std::array<int, 3> arr3{{1, 2, 3}}; /*4*/ std::array<int, 3> arr4 = {1, 2, 3}; /*5*/ std::array<int, 3> arr5 = {{1, 2, 3}}; /*6*/ std::array<int, 3> arr6 = std::array<int, 3>({1, 2, 3}); /*7*/ std::array<int, 3> arr7 = std::array<int, 3>({{1, 2, 3}}); What are the correct ones

Initializing an std::array of non-default-constructible elements?

微笑、不失礼 提交于 2019-11-30 03:51:39
问题 Suppose type foo_t with a named constructor idiom, make_foo() . Now, I want to have exactly 123 foo's - no more, no less. So, I'm thinking about an std::array<foo_t, 123> . Now, if foo_t were default-constructible, I would write: std::array<foo_t, 123> pity_the_foos; std::generate( std::begin(pity_the_foos), std::end(pity_the_foos), []() { return make_foo(); } ); and Bob's my uncle, right? Unfortunately... foo_t has no default ctor. How should I initialize my array, then? Do I need to use

constexpr std::array with static_assert

我只是一个虾纸丫 提交于 2019-11-30 02:40:27
问题 #include <iostream> #include <array> int main(int argc, char **argv) { constexpr const std::array<int, 2> arr {{ 0, 1 }}; constexpr const int arr2[] = { 0, 1}; static_assert(arr[0] == arr2[0], "asdf"); static_assert(arr[1] == arr2[1], "asdfasdf"); return 0; } When compiled with gcc 4.8.2 and 4.9.1 using g++ test.cpp --std=c++11 , the compilation succeeds. When compiled with clang 3.4 and 3.5 using clang++ test.cpp --std=c++11 however, the compilation fails: test.cpp:8:16: error: static_assert

Is there a way to enforce full initialization of std::array

痞子三分冷 提交于 2019-11-29 18:43:12
问题 I am using std::array<size_t, N> (N is a fixed template-variable). #include<array> template<size_t N> struct A{ size_t function(std::array<size_t, N> arr){ return arr[N-1];} // just an example }; int main(){ A<5> a; a.function({{1,2,3,4,5}})); } And it works fine. The problem is that this other code is silently allowed: A.function({{1,2,3}})); That is, even with missed elements the array is initialized somehow, even if it is well defined (e.g. remaining elements initialized to zero, I am not

Initializing a std::array with a constant value

不羁岁月 提交于 2019-11-29 16:23:00
问题 I need to initialize all elements of a std::array with a constant value, like it can be done with std::vector . #include <vector> #include <array> int main() { std::vector<int> v(10, 7); // OK std::array<int, 10> a(7); // does not compile, pretty frustrating } Is there a way to do this elegantly? Right now I'm using this: std::array<int, 10> a; for (auto & v : a) v = 7; but I'd like to avoid using explicit code for the initialisation. 回答1: With std::index_sequence , you might do: namespace

What is the sizeof std::array<char, N>? [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-29 13:45:11
This question already has an answer here: Is the size of std::array defined by standard 1 answer What does the C++ standard say about what sizeof(std::array<char, N>) should be (for some constant N )? In a comment to a different question , it was mentioned that std::array is not always "stack allocated". The comment was in response to a different comment that speculated that putting a too large of a constant for std::array that is declared as a local variable could cause the program to abort due to insufficient resources for the "stack allocated" variable. I assume the followup comment meant

c++11 array initialization won't call copy constructor

倾然丶 夕夏残阳落幕 提交于 2019-11-28 11:43:14
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} { } In the member initialization of the

Array declaration and initialization in C++11

≯℡__Kan透↙ 提交于 2019-11-28 11:04:06
Here are 8 ways to declare and initialize arrays in C++11 that seems ok under g++ : /*0*/ std::array<int, 3> arr0({1, 2, 3}); /*1*/ std::array<int, 3> arr1({{1, 2, 3}}); /*2*/ std::array<int, 3> arr2{1, 2, 3}; /*3*/ std::array<int, 3> arr3{{1, 2, 3}}; /*4*/ std::array<int, 3> arr4 = {1, 2, 3}; /*5*/ std::array<int, 3> arr5 = {{1, 2, 3}}; /*6*/ std::array<int, 3> arr6 = std::array<int, 3>({1, 2, 3}); /*7*/ std::array<int, 3> arr7 = std::array<int, 3>({{1, 2, 3}}); What are the correct ones according to the strict standard (and the upcoming C++14 standard) ? What are the most common/used and

std::array with aggregate initialization on g++ generates huge code

不问归期 提交于 2019-11-28 07:23:48
On g++ 4.9.2 and 5.3.1, this code takes several seconds to compile and produces a 52,776 byte executable: #include <array> #include <iostream> int main() { constexpr std::size_t size = 4096; struct S { float f; S() : f(0.0f) {} }; std::array<S, size> a = {}; // <-- note aggregate initialization for (auto& e : a) std::cerr << e.f; return 0; } Increasing size seems to increase compilation time and executable size linearly. I cannot reproduce this behaviour with either clang 3.5 or Visual C++ 2015. Using -Os makes no difference. $ time g++ -O2 -std=c++11 test.cpp real 0m4.178s user 0m4.060s sys

How to create std::array with initialization list without providing size directly [duplicate]

久未见 提交于 2019-11-27 11:46:56
This question already has an answer here: How to emulate C array initialization “int arr[] = { e1, e2, e3, … }” behaviour with std::array? 9 answers How can I make a3 compile? int main() { int a1[] = { 1, 2, 3 }; std::array<int, 3> a2 = { 1, 2, 3 }; std::array<int> a3 = { 1, 2, 3 }; } It's very inconvenient, and brittle, to hard-code the size of the array when using an initialization list, especially long ones. Is there any work around? I hope so otherwise I'm disappointed because I hate C arrays and std::array is supposed to be their replacement. There is currently no way to do this without