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
8
votes
1 answer

The deduction guide for std::array

In the C++ 17 and C++ 20 Working Drafts of the C++ Standard the deduction guide for the class template std::array is defined the following way template array(T, U...) -> array; As a result for example this…
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
8
votes
1 answer

Why is size_type in std::array size_t and in std::vector usually size_t?

The documentation says, that size_type of std::vector is /usually/ size_t, which is reasonable, since an implementation can choose to use different. But why is size_type = size_t in std::array. Especially here, as std::array is used on small µC a…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39
8
votes
3 answers

Can C++ raise an error when std array initialization is too small?

Suppose I have an array of: std::array {4,3,2}; Is it possible to raise an error or warning when this is the case? In some cases it might be useful to have this explicitly matching.
Marnix
  • 6,384
  • 4
  • 43
  • 78
8
votes
0 answers

Why does gcc take over 17 minutes to compile std::array?

Why does std::array a = {}; take more than 17 minutes to compile with gcc using -O3 on a Core i7 9700K processor? I've stumbled on a problem with gcc where I've empty-initialized a std::array with std::strings in the header file,…
Ingemar
  • 109
  • 2
8
votes
2 answers

Is it possible to move an std::array into a std::vector?

This is a question about the interaction of stack memory and heap memory and the particular case of going from stack to heap via the std::array and std::vector classes. In principle std::array can be seen as a pointer to the first elements, plus…
alfC
  • 14,261
  • 4
  • 67
  • 118
8
votes
2 answers

std::array of size zero

What does it mean to have std::array,array of size zero? I have gone through similar questions in SO before posting this, and all those questions are regarding simple array type and for C language and most of them says that it is illegal. But…
Sreeraj Chundayil
  • 5,548
  • 3
  • 29
  • 68
8
votes
3 answers

Class with private constructor and static array of itself

Sorry if title is confusing, I couldn't find an easy way to write it in a simple sentence. Anyways, the issue I'm facing: // header: class SomeThing { private: SomeThing() {} // <- so users of this class can't come up // …
Gábor Buella
  • 1,840
  • 14
  • 22
8
votes
2 answers

Converting std::array to std::vector

In the code below, the size of the function (foo) argument (std::vector) can be anything to make the function a generic one. However, sometimes the size container is known so std::array can be used. The problem is to convert the std::array to…
Shibli
  • 5,879
  • 13
  • 62
  • 126
7
votes
4 answers

What is the difference between [start/2 + mid/2] and [(start + mid)/2] in binary search?

In the binary search algorithm, we set the mid as: mid = (start + end)/2 which is same as mid = start/2 + end/2 and also equal to mid = start + (end - start)/2 but all of the three give different results being the same arithmetic expression. How…
user19117411
  • 105
  • 5
7
votes
1 answer

Is it possible to emplace a std::array in a container? If it is how? If not, why?

Given the simple code #include #include int main() { std::vector> v; v.emplace_back(std::array{1,2,3}); } I'm first of all worried about what is really going on. My understanding of emplace_back is…
Enlico
  • 23,259
  • 6
  • 48
  • 102
7
votes
2 answers

Size of reference to std::array not available at compiletime

I'm interested to know why the second static_assert in my code below doesn't work. It seems like even though the array c is a reference to a, the size of the array is embedded in the type, so it should be available at compile time. #include…
Jacob Merson
  • 130
  • 5
7
votes
3 answers

In C++ what is the point of std::array if the size has to be determined at compile time?

Pardon my ignorance, it appears to me that std::array is meant to be an STL replacement for your regular arrays. But because the array size has to be passed as a template parameter, it prevents us from creating std::array with a size known only at…
Plasty Grove
  • 2,807
  • 5
  • 31
  • 42
7
votes
1 answer

how convert std::array to char (&dest)[N]?

What is the way to pass std::array to such function: template void safe_func(char (&dest)[N]); ? I try this one: #include template using SafeArray = char[N]; template void safe_func(char…
user1244932
  • 7,352
  • 5
  • 46
  • 103
7
votes
2 answers

Why are "double braces" needed in declaration of multi-dimensional array using stacked std::array?

#include using std::array; constexpr auto d1=2; constexpr auto d2=3; constexpr auto d3=4; // stacked std::array using arr_t = array; using arr2d_t = array; using arr3d_t = array; constexpr arr3d_t arr1 = {{ …
sandthorn
  • 2,770
  • 1
  • 15
  • 59
7
votes
2 answers

Why isn't std::array's operator==() marked constexpr?

It's very natural to want to compare std::array's at compile time; and its operator==() is obviously constexpr'able. Yet - it isn't marked constexpr. Is this intentional or an oversight? And - what's the reason it was left that way (apparently in…
einpoklum
  • 118,144
  • 57
  • 340
  • 684