Questions tagged [stdarray]

std::array is a container that encapsulates constant size arrays in C++.

std::array is a part of the C++ Standard Library since C++11.

Reference: https://en.cppreference.com/w/cpp/container/array

433 questions
11
votes
5 answers

fill std::array in the member initialization list

The following code works but I would like to avoid the warning: warning: 'fitness::vect_' should be initialized in the member initialization list [-Weffc++] when it is compiled with the g++ -Weffc++ switch: #include template
manlio
  • 18,345
  • 14
  • 76
  • 126
11
votes
4 answers

Array declaration and initialization in C++11

Here are 8 ways to declare and initialize arrays in C++11 that seems ok under g++: /*0*/ std::array arr0({1, 2, 3}); /*1*/ std::array arr1({{1, 2, 3}}); /*2*/ std::array arr2{1, 2, 3}; /*3*/ std::array arr3{{1, 2,…
Vincent
  • 57,703
  • 61
  • 205
  • 388
10
votes
1 answer

std class specialization - meeting the standard library requirements for the original std::array template

In Is it safe to define a specialization for std::array the questioner asks if it's safe to specialize std::array for a program defined type which has an expensive default constructor. The goal is to instead initialize all elements in the std::array…
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
10
votes
4 answers

Compile time size of data member std::array in non-class template (C++14)

Compile-time inspection std::array data member for its (compile time) size I need to statically assert that the compile-time size of a non-constexpr data member of type std::array, say arr_, of a non-template class is equal to a given (externally…
dfrib
  • 70,367
  • 12
  • 127
  • 192
10
votes
3 answers

C++ performance std::array vs std::vector

I know C-style arrays or std::array aren't faster than vectors. I use vectors all the time (and I use them well). However, I have some situation in which the use of std::array performs better than with std::vector, and I have no clue why (tested…
mfnx
  • 2,894
  • 1
  • 12
  • 28
10
votes
1 answer

Multiplying each element of an std::array at compile time

I would like to convert an std::array to another std::array, multiplying each of its elements by a specific number. What I have right now obviously doesn't work: #include #include #include template
syntagma
  • 23,346
  • 16
  • 78
  • 134
10
votes
1 answer

constexpr std::array with static_assert

#include #include int main(int argc, char **argv) { constexpr const std::array arr {{ 0, 1 }}; constexpr const int arr2[] = { 0, 1}; static_assert(arr[0] == arr2[0], "asdf"); static_assert(arr[1] ==…
inetknght
  • 4,300
  • 1
  • 26
  • 52
10
votes
2 answers

How does std::array initializer work for char's?

I'm not sure how the following code works. I thought you had to do {'h', 'e' ...etc...} but it seems to work fine. On the other hand if you do std::array
user4051846
  • 103
  • 1
  • 5
10
votes
1 answer

Initializing std::array member in constructor using string literal. GCC bug?

The following example initializing a std::array member in a constructor using a string literal doesn't compile on GCC 4.8 but compiles using Clang 3.4. #include #include struct A { std::array x; …
Ricky65
  • 1,657
  • 18
  • 22
9
votes
1 answer

Why can't a 2D std::array be initialized with two layers of list-initializers?

can someone help me understand why my compiler can't/doesn't deduce this? (using g++ 7.3) Does not work: #include std::array,2> f() { return {{0,0},{0,0}}; } Works fine: #include…
Peter Mitrano
  • 2,132
  • 28
  • 31
9
votes
2 answers

Initializing private std::array member in the constructor

I was wondering what is the proper way to initialize a std::array member of the class in the constructor, when the initial array values are parameters to the constructor? More specifically, consider the following example: class Car { public: …
MikeL
  • 2,369
  • 2
  • 24
  • 38
9
votes
4 answers

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

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.…
Jersey
  • 423
  • 4
  • 13
9
votes
6 answers

Simplest way to get memory size of std::array's underlying array?

Is this the simplest/shortest way to get size in memory of the content of what std::array::data() returns? arr.size() * sizeof(arr.value_type) Edit: My question wasn't precise. By "size in memory" I mean size of all elements (themselves) contained…
NPS
  • 6,003
  • 11
  • 53
  • 90
9
votes
2 answers

initialize std::array without copying/moving elements

#include #include #include class C { private: std::string a; std::string b; std::string c; public: C(std::string a_, std::string b_, std::string c_) : a{a_},b{b_},c{c_} {} ~C(){}; C(const C&)…
Marinos K
  • 1,779
  • 16
  • 39
9
votes
4 answers

How do you declare a pointer to a C++11 std::array?

Depending on a variable, I need to select the SeedPositions32 or SeedPositions16 array for further use. I thought a pointer would allow this but I can't seed to make it work. How do you declare a pointer to a C++11 std::array? I tried the…
whitebloodcell
  • 308
  • 1
  • 4
  • 10