stdarray

Initialisation of std::array<>

最后都变了- 提交于 2019-12-04 08:24:29
问题 Consider the following code: #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 } }; static std::array<A, 4> x4 = { A{ 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; Compiling with gcc: $ gcc -c --std=c++11 array.cpp array.cpp:15:1: error: too many initializers for ‘std::array<A, 4ul>’ }

Initializing std::array<char,x> member in constructor using string literal. GCC bug?

こ雲淡風輕ζ 提交于 2019-12-04 01:54:07
The following example initializing a std::array <char, N> member in a constructor using a string literal doesn't compile on GCC 4.8 but compiles using Clang 3.4. #include <iostream> #include <array> struct A { std::array<char, 4> x; A(std::array<char, 4> arr) : x(arr) {} }; int main() { // works with Clang 3.4, error in GCC 4.8. // It should be the equivalent of "A a ({'b','u','g','\0'});" A a ({"bug"}); for (std::size_t i = 0; i < a.x.size(); ++i) std::cout << a.x[i] << '\n'; return 0; } On first impression it looks like a GCC bug. I feel it should compile as we can initialize a std::array

Initialisation of std::array<>

不羁的心 提交于 2019-12-02 22:58:57
Consider the following code: #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 } }; static std::array<A, 4> x4 = { A{ 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; Compiling with gcc: $ gcc -c --std=c++11 array.cpp array.cpp:15:1: error: too many initializers for ‘std::array<A, 4ul>’ }; ^ $ NB1: Commenting out the first initialisation statement, the code compiles without error. NB2:

Trying to pass a constexpr lambda and use it to explicitly specify returning type

走远了吗. 提交于 2019-12-02 04:57:51
问题 I would like to use a function and pass a constexpr lambda . However, it only compiles successfully if I let the type be deduced through auto . Explicitly giving the type through -> std::array<event, l()> seems to fail (the first instance). Why is this? template <typename Lambda_T> constexpr static auto foo(Lambda_T l) -> std::array<event, l()> { return {}; } // error template <typename Lambda_T> constexpr static auto foo(Lambda_T l) { return std::array<event, (l())>{}; } // OK template

Trying to pass a constexpr lambda and use it to explicitly specify returning type

点点圈 提交于 2019-12-02 02:18:06
I would like to use a function and pass a constexpr lambda . However, it only compiles successfully if I let the type be deduced through auto . Explicitly giving the type through -> std::array<event, l()> seems to fail (the first instance). Why is this? template <typename Lambda_T> constexpr static auto foo(Lambda_T l) -> std::array<event, l()> { return {}; } // error template <typename Lambda_T> constexpr static auto foo(Lambda_T l) { return std::array<event, (l())>{}; } // OK template <typename Lambda_T> constexpr static auto foo(Lambda_T l) -> decltype(l()) { return {}; } // OK Note that,

Why can't I decrement std::array::end()?

核能气质少年 提交于 2019-12-01 03:41:14
I'm creating a convenient display() function template for container types. The output for the last element is different from the rest, thus I check when myIterator != --cont.cend(); . This works for std::vector , but won't work for std::array . Why? Here's a MWE (not my actual code): std::vector<double> vec({1,2}); std::array<double, 2> arr({{1,2}}); auto vecIt = --vec.end(); // OK auto arrIt = --arr.end(); // error: lvalue required as decrement operand It depends on how the iterator is defined. It seems that for the class template std::array the iterator is defined as a pointer. So the

Populate std::array with non-default-constructible type (no variadic templates)

落爺英雄遲暮 提交于 2019-12-01 00:34:45
Suppose I have a type A with no default constructor: struct A { int x; A(int x) : x(x) {} }; I want to make an std::array of A . I can easily make it with initializer list: std::array<A, 5> arr = { 0, 1, 4, 9, 16 }; You can see a pattern here. Yes, I can have a generator function to compute each value of the array: int makeElement(size_t i) { return i * i; } std::array<A, 5> arr = { makeElement(0), makeElement(1), makeElement(2), makeElement(3), makeElement(4) }; And yes, in fact I have much more than 5 elements (64, namely). So it would be nice not to repeat makeElement 64 times. The only

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

房东的猫 提交于 2019-11-30 20:17:26
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 some variadic template expansion voodoo perhaps? Note: Answers may use anything in C++11, C++14 or C++17

Populate std::array with non-default-constructible type (no variadic templates)

南楼画角 提交于 2019-11-30 19:05:42
问题 Suppose I have a type A with no default constructor: struct A { int x; A(int x) : x(x) {} }; I want to make an std::array of A . I can easily make it with initializer list: std::array<A, 5> arr = { 0, 1, 4, 9, 16 }; You can see a pattern here. Yes, I can have a generator function to compute each value of the array: int makeElement(size_t i) { return i * i; } std::array<A, 5> arr = { makeElement(0), makeElement(1), makeElement(2), makeElement(3), makeElement(4) }; And yes, in fact I have much

Initializing a std::array with a constant value

半城伤御伤魂 提交于 2019-11-30 11:02:57
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. With std::index_sequence , you might do: namespace detail { template <typename T, std::size_t ... Is> constexpr std::array<T, sizeof...(Is)> create_array(T value