stdarray

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

ぐ巨炮叔叔 提交于 2019-12-08 17:40:04
问题 #include <array> using std::array; constexpr auto d1=2; constexpr auto d2=3; constexpr auto d3=4; // stacked std::array using arr_t = array<int,d1>; using arr2d_t = array<arr_t,d2>; using arr3d_t = array<arr2d_t,d3>; constexpr arr3d_t arr1 = {{ {{ {1,2}, {3,4}, {5,6} }}, {{ {1,2}, {3,4}, {5,6} }}, {{ {1,2}, {3,4}, {5,6} }}, {{ {1,2}, {3,4}, {5,6} }} }}; // built-in array using carr3d_t = int[d3][d2][d1]; constexpr carr3d_t arr2 = { { {1,2}, {3,4}, {5,6} }, { {1,2}, {3,4}, {5,6} }, { {1,2}, {3

Initializing private std::array member in the constructor

好久不见. 提交于 2019-12-07 10:20:27
问题 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: Car(const std::string& color, int age): color_(color), age_(age) {} // ... private: std::string color_; int age_; }; class ThreeIdenticalCars { private: std::array<Car, 3> list; public: ThreeIdenticalCars(const std::string& color, int age): // What to put

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

做~自己de王妃 提交于 2019-12-07 02:40:55
问题 What is the way to pass std::array<char, N> to such function: template<size_t N> void safe_func(char (&dest)[N]); ? I try this one: #include <array> template <size_t N> using SafeArray = char[N]; template <size_t N> void safe_func(char (&dest)[N]) {} int main() { SafeArray<10> a1; safe_func(a1); std::array<char, 10> a2; safe_func(*static_cast<SafeArray<10> *>(static_cast<void *>(a2.data()))); } It works, but I doubt, may be something wrong with my cast, and on other compiler or platform (I

initialize std::array without copying/moving elements

…衆ロ難τιáo~ 提交于 2019-12-07 00:33:22
问题 #include <iostream> #include <string> #include <array> 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&) =delete; C(const C&&) =delete; const C& operator=(const C&) =delete; const C& operator=(const C&&) =delete; }; std::array<C,2> array = {C("","",""),C("","","")}; int main() {} this won't compile (Android Studio with NDK and clang) with a "call to deleted constructor of c"

Unneeded conversion happening on std::move

一笑奈何 提交于 2019-12-06 15:11:15
Why is the compiler thinking I am trying to convert from an object of type Container to an object of type MoveTest in the following code? Why is the braced constructor being forwarded to the move_things variable? Replacing the {} with () of course solves the problem #include <iostream> #include <array> using namespace std; static constexpr int NUMBER{2}; class MoveTest { public: MoveTest(const MoveTest&) { cout << "MoveTest(const MoveTest&)" << endl; } MoveTest(MoveTest&&) { cout << "MoveTest(MoveTest&&)" << endl; } MoveTest() { cout << "MoveTest()" << endl; } }; class Container { public:

Initializing std::array<char,x> member in constructor using string literal. GCC bug?

99封情书 提交于 2019-12-05 17:06:09
问题 The following example initializing a std::array <char, N> member in a constructor using a string literal doesn't compile on GCC 4.8 but compiles using Clang 3.4. #include <iostream> #include <array> struct A { std::array<char, 4> x; A(std::array<char, 4> arr) : x(arr) {} }; int main() { // works with Clang 3.4, error in GCC 4.8. // It should be the equivalent of "A a ({'b','u','g','\0'});" A a ({"bug"}); for (std::size_t i = 0; i < a.x.size(); ++i) std::cout << a.x[i] << '\n'; return 0; } On

Initializing private std::array member in the constructor

淺唱寂寞╮ 提交于 2019-12-05 12:44:01
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: Car(const std::string& color, int age): color_(color), age_(age) {} // ... private: std::string color_; int age_; }; class ThreeIdenticalCars { private: std::array<Car, 3> list; public: ThreeIdenticalCars(const std::string& color, int age): // What to put here to initialize list to 3 identical Car(color,age) objects? {} }; Obviously one way is to write

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

北城余情 提交于 2019-12-05 08:38:54
What is the way to pass std::array<char, N> to such function: template<size_t N> void safe_func(char (&dest)[N]); ? I try this one: #include <array> template <size_t N> using SafeArray = char[N]; template <size_t N> void safe_func(char (&dest)[N]) {} int main() { SafeArray<10> a1; safe_func(a1); std::array<char, 10> a2; safe_func(*static_cast<SafeArray<10> *>(static_cast<void *>(a2.data()))); } It works, but I doubt, may be something wrong with my cast, and on other compiler or platform (I used gcc/linux/amd64), I faced with wrong reference? One way: template<class T, size_t N> using c_array =

initialize std::array without copying/moving elements

守給你的承諾、 提交于 2019-12-05 05:28:30
#include <iostream> #include <string> #include <array> 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&) =delete; C(const C&&) =delete; const C& operator=(const C&) =delete; const C& operator=(const C&&) =delete; }; std::array<C,2> array = {C("","",""),C("","","")}; int main() {} this won't compile (Android Studio with NDK and clang) with a "call to deleted constructor of c" error. I know that I can e.g. use a std::vector and emplace_back() to construct an element directly inside

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

倖福魔咒の 提交于 2019-12-05 03:27:10
can someone help me understand why my compiler can't/doesn't deduce this? (using g++ 7.3) Does not work: #include <array> std::array<std::array<double,2>,2> f() { return {{0,0},{0,0}}; } Works fine: #include <array> std::array<std::array<double,2>,2> f() { return {std::array<double,2>{0,0},{0,0}}; } Also weirdly this fails too: #include <array> std::array<std::array<double,2>,2> f() { return std::array<std::array<double,2>,2>{{0,0},{0,0}}; } @1201ProgramAlarm pointed out that adding another set of curly braces works: #include <array> std::array<std::array<double,2>,2> f() { return {{{0,0},{0,0