stdarray Questions

3

Solved

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...
Lime asked 20/8, 2017 at 14:45

3

Solved

In the question Idiom for initializing an std::array using a generator function taking the index?,which basically asks how one can initialize an array of an arbitrary type which is not necessarily ...
Gribble asked 26/6 at 14:23

2

Suppose I have a function T foo(size_t i). What would be an elegant and succinct way of constructing an object arr, of type std::array<T, N>, so that we have arr[i] == foo(i)? If possible, I ...
Amagasaki asked 26/6 at 12:28

2

Solved

In C++ I have this struct from C. This code is very old and cannot be modified: struct Point { double coord[3]; }; On the other hand, I have this modern function which returns modern std::array i...
Tetramethyldiarsine asked 19/12, 2023 at 8:56

2

Solved

I have a function that takes an std::array of given size N void func(std::array<int,3> x) { // do something } int main() { func({4,4,4}) // works func({4}) // works as well } I understan...
Apotheosis asked 17/10, 2023 at 11:9

7

Solved

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 memor...
Angele asked 27/9, 2017 at 13:4

2

Solved

In C++14, how do I initialize a global constexpr std::array of std::pair containing text strings? The following doesn't work: #include <array> constexpr std::array<std::pair<int, cons...
Camass asked 11/3, 2019 at 10:32

8

Solved

If I want to build a very simple array like: int myArray[3] = {1, 2, 3}; Should I use std::array instead? std::array<int, 3> a = {{1, 2, 3}}; What are the advantages of using std::array ove...
Lamoreaux asked 15/5, 2015 at 15:27

8

Solved

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> ...
Freshman asked 2/9, 2019 at 11:56

3

Solved

I'm about to convert a lot of old C++ code to more modern C++. There are many raw 2D arrays in that code like: Foo bar[XSIZE][YSIZE]; And I'm about to replace these declarations with std::array&lt...
Tanh asked 8/8, 2023 at 13:40

3

Solved

I am trying to create a std::array<uint8_t,N> from a std::span<uint8_t,N> but I cannot find a way to do so without memcpy, std::copy, or std::ranges::copy which don't protect me against...
Sherrer asked 28/7, 2023 at 18:9

4

Solved

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 r...
Hebe asked 10/7, 2023 at 11:25

4

Solved

C++17 allows us to have std::array's template arguments deduced. E.g., I can write std::array ints = { 1, 2, 3 }; and ints will be of type std::array<int, 3>. My question is: what if I wa...
Dejecta asked 10/1, 2019 at 9:26

1

Solved

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 ...
Merozoite asked 21/12, 2022 at 18:34

1

Solved

In libc++, the specialization of std::array<T,0> has a member (const) char array, which is aligned and sized according to T (source). I wonder what is the reason for this implementation since...
Taco asked 11/5, 2023 at 8:27

2

Solved

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 <size_t Rows, size_t Columns, class T=double> struct Matrix : p...
Fayre asked 2/8, 2020 at 0:7

1

Solved

C++20 added std::to_array so you can easily create std::array from a C-Style array, for example: template<typename T, std::size_t N> void foo(const T (&a)[N]) { auto arr = std::to_array(...
Jaunty asked 5/3, 2023 at 7:52

2

I have a type declared as: using Buffer = std::unique_ptr<std::array<uint8_t, N>>; I also have a template function declared as: template<typename Buffer> bool temp_func() { // d...
Taka asked 25/2, 2023 at 23:6

3

How can I write an std::array concatenation function? template <typename T, std::size_t sza, std::size_t szb> std::array<T, sza+szb> concat (const std::array<T, sza>& aa, co...
Toluene asked 6/2, 2023 at 7:57

5

Solved

There are many design issues I have found with this, particularly with passing std::array<> to functions. Basically, when you initialize std::array, it takes in two template parameters, <c...
Manus asked 26/1, 2023 at 2:39

2

Solved

I am working on a library that has a class foo. foo has a non-trivial constructor. When I create an std::array of foo (std::array<foo, 10>), the constructor is called 10 times. I want to impl...
Dab asked 20/12, 2022 at 19:34

2

Solved

Can I use a std::array<int, N> 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 semanti...
Dasha asked 19/12, 2022 at 21:16

4

Solved

In the following code: template<size_t N> int b(int q, const std::array<int, N>& types) { int r = q; for (int t : types) { r = r + t; } return r; } int main() { b<2>(9...

4

Solved

In C++11, using lambda/for_each, how do we iterate an array from end? I tried the following, but both result in infinite loop: for_each (end(A), begin(A), [](int i) { .... }); for_each (A.rend(...
Debunk asked 1/9, 2013 at 20:7

3

Solved

The following code is returning the compilation error below. I'm stuck understanding how there are too many initializers. This code works using vector<X>. Does anyone know why the error is be...
Salmonella asked 7/6, 2020 at 21:44

© 2022 - 2024 — McMap. All rights reserved.