467 questions
15
votes
4
answers
1k
views
What is the size of std::array<T,0>?
Unlike C array, std::array is allowed to have zero length. But the current implementations differ in how std::array<T,0> is implemented, in particular the sizes of the object are different.
This ...
1
vote
2
answers
161
views
C++: std::array invalid array assignment [duplicate]
I have the below code. What am I doing wrong that I can't simple assign the array myBar to the 1st index of foo? Using memcpy works but somehow I don't want to use this approach.
#include <array>...
12
votes
7
answers
2k
views
Construct an std::array at compile time in C++17 using the preprocessor
I am developing a C++17 framework that has optional third-party dependencies and I'd like to construct an std::array that contains the names of the available dependencies.
The CMake file sets ...
4
votes
1
answer
165
views
Why does template deduction for a nested std::array with one element result in a one dimensional std::array?
The following code compiles successfully with clang and gcc. Naively, I would have expected a2 to have type std::array<std::array<int, 1>, 1>.
#include <array>
auto a1 = std::array{...
0
votes
2
answers
171
views
Cannot define an `std::array` with `std::string` argument in Visual C++
This code builds successfully with Clang 20.1.10 and with GCC 15.1, but not with Microsoft Visual C++ 2022 version 17.14.0:
#include <array>
#include <string>
int main()
{
static ...
7
votes
1
answer
235
views
Directly initialize std::array with a fixed count of elements from std::vector [duplicate]
The elements in the array are not default constructible therefore we need to initialize the array directly from elements in std::vector.
#include <array>
#include <vector>
class foo {
...
9
votes
0
answers
177
views
std::to_array differs from direct initialization
Initially I thought that std::to_array would be equivalent to initializing directly, i.e. a way to avoid having to specify the length of the array in the code, compare:
constexpr auto arr = std::...
2
votes
1
answer
363
views
Using std::ranges::to with std::array
I would like to have this code with std::vector replaced with std::array, but I presume it can not be done since std::array is not a container std::ranges::to understands.
constexpr auto dice = std::...
41
votes
1
answer
3k
views
Which feature of C++23 allows converting std::array to std::tuple?
The following code snippet implicitly converts a std::array to a std::tuple:
std::array<int,3> arr = {2,4,6};
std::tuple<int,int,int> tup;
tup = arr; // c++23 or later
The assignment ...
1
vote
4
answers
214
views
How to split std::array without copying?
I need to access an std::array<double, 9> x by two functions, namely funA and funB. funA only needs the first 6 entries of x and funB the last 3 entries. I am limited to C++14.
I know there are ...
2
votes
0
answers
60
views
Nested std::array of primitive type initialization to default value
We know std::array<double, N> Can be initialized to all-zero by
std::array<double, N> my_array {}
My question is if I have
std::array<std::array<double, N>, M> my_array {}
...
15
votes
3
answers
928
views
How to use std::array.size() as a template parameter when a class has a non-constexpr std::array
The following is a toy example
The class student has a std::array<char, 15> called name and an integer age. A student has a member function called encode that calls a global template function ...
3
votes
2
answers
135
views
initializing std::array with nullptr without template parameters
I am using an API that expects a contiguous block of memory that contains pointers.
the pointers themselves can be nullptr.
Currently, I use C-Arrays:
ID3D11ShaderResourceView* srvs[] = {
...
1
vote
1
answer
164
views
std::span on containers of contiguous data
std::span works great on std::vectors, std::arrays or c pointers. However, often the contiguous memory has a different type associated to it such as for example a vector of arrays. I'm trying to use a ...
4
votes
1
answer
168
views
Why doesn't std::array's operator[] retain the value category of the array?
I'd have expected that if an std::array is an rvalue, then calling operator[] returns an rvalue as well. But it doesn't seem to be the case (cppreference), there are no value-category overloads.
For ...