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
7
votes
2 answers

Initialize a two-dimensional std::array of type enum class (C++11)

I have the following class in C++11: class MyTable { public: enum class EntryType { USED, FREE }; MyTable(EntryType value) { for (uint32_t i = 0; i < 10; ++i) { memset(_table[i].data(),…
itzhaki
  • 317
  • 2
  • 10
6
votes
2 answers

Assigning a subsection of C-style array using a std::array& without violating "strict aliasing" and hence invoking UB?

Can I use a std::array to alias parts of a int[] without invoking UB? https://en.cppreference.com/w/cpp/container/array "This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only…
Oliver Schönrock
  • 1,038
  • 6
  • 11
6
votes
2 answers

sfinae to detect containers: failure for std:array

I am looking for a way to use SFINAE to implement some function, that must be available only to some containers: vector, list, array (set is there below only as a test) Build upon this answer, I tried the code below that uses a traits class that…
kebs
  • 6,387
  • 4
  • 41
  • 70
6
votes
2 answers

How to append or insert std::array elements into a std::vector?

I'm relatively new to c++ and I've tried to do some research, but while searching online I've mainly come across C arrays rather than std::array. What are the most efficient ways to append std::array elements into a std::vector, and to insert…
spaL
  • 604
  • 7
  • 21
6
votes
2 answers

How can I construct an std::array filled with some uniform value?

std::array can be constructed (at compile time with newer C++ versions) with specific values, e.g. std::array a{1, 4, 9}; however - it does not have a constructor, or a standard-library named constructor idiom, taking a single value and replicating…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
6
votes
2 answers

Automatic size deduction for two-dimensional array

I am trying to make a fixed-size matrix class. The intent is to make it inherit or utilize a std::array of std::array: template struct Matrix : public std::array, Rows> { }; I…
Peter Moran
  • 295
  • 3
  • 13
6
votes
3 answers

C++: array<> too many initializers

The following code is returning the compilation error below. I'm stuck understanding how there are too many initializers. This code works using vector. Does anyone know why the error is being reported and how to resolve? Thanks #include…
notaorb
  • 1,944
  • 1
  • 8
  • 18
6
votes
1 answer

Heap allocation for std::array

According to this question std::array is allocated on the stack. However when using it together with Valgrind it shows me a heap allocation, even for elements which are allocated on the stack. Is this a false positive or real? Here follow two mwe to…
magu_
  • 4,766
  • 3
  • 45
  • 79
6
votes
1 answer

Is it beneficial anymore to unroll loops in C++ over fixed-sized arrays?

I want to use the std::array to store the data of N-dimensional vectors and implement arithmetic operations for such vectors. I figured, since the std::array now has a constexpr size() member function, I can use this to unroll the loops that I need…
tmaric
  • 5,347
  • 4
  • 42
  • 75
6
votes
1 answer

C++ - Definition of 2d matrices of type std::array

I am looking to define two 2d matrices: f and f_transpose of the type: std::array , dim> and std::array , 3>. The value of dim is 23. I want elements f[0][0], f[1][1], f[2][2], f_transpose[0][0],…
skr
  • 914
  • 3
  • 18
  • 35
6
votes
1 answer

Use std::vector for std::array initialization

Suppose I have a std::vector of a size known at compile time, and I want to turn that into an std::array. How would I do that? Is there a standard function to do this? The best solution I have so far is this: template
MvG
  • 57,380
  • 22
  • 148
  • 276
6
votes
1 answer

Using std::array as Attribute for boost::spirit::x3

I'm trying to parse a list of numbers into a fixed sized std::array container using boost::spirit's newest release x3 (as included in boost 1.54). Since std::array has the necessary functions, it is detected as an Container, but it is lacking the…
ithron
  • 310
  • 3
  • 9
6
votes
1 answer

creating an std::array with size calculated during run time

I want to create an object of std::array but the problem is I can only use functions that return a constexprtype or compiler will complain. The problem here is that I need to calculate the length of this array based on another array's size…
max
  • 2,627
  • 1
  • 24
  • 44
6
votes
2 answers

How to properly static cast a vector in C++?

I have a code in which at the end of a function I need to cast from int to double all the elements of an array in order to being able to do a final push_back before exiting the function. The code I have right now is: template
Javi
  • 3,440
  • 5
  • 29
  • 43
5
votes
2 answers

get the size of std::array inside unique_ptr without an instance in c++

I have a type declared as: using Buffer = std::unique_ptr>; I also have a template function declared as: template bool temp_func() { // do something } and I'm calling temp_func with type…
אנונימי
  • 304
  • 2
  • 7