Category : aggregate-initialization

With std::array you can do std::array<int,3> v = {1,2,3}; But I want to provide a wrapper template <typename T, int n> circular_array { auto operator[](size_t i){ return m_data[i%n]; } // TODO forward all other methods directly except “at“ which we don’t need private: std::array<t,n> m_data; } but I lose the aggregate initialization that I had ..

Read more

I’ve run into an interesting warning (with GCC and clang -Wnarrowing at least, haven’t tested other compilers), when aggregate initialising a struct using a ternary operator expression (see below code). I don’t understand why the type of the expression is apparently considered int in the aggregate initialisation case, but is silently coerced to be a ..

Read more

I want to make my own struct for a 3×3 Matrix. I want to allow construction via components/elements or by "rows". So either you provide a std::array<float, 9> or a std::array<std::array<float, 3>, 3> However when defining the struct like this with the following constructors: struct Matrix3x3 { Matrix3x3(std::array<float, 9> components) { } Matrix3x3(std::array<std::array<float, 3>, 3> ..

Read more

I want to make my own struct for a 3×3 Matrix. I want to allow construction via components/elements or by "rows". So either you provide a std::array<float, 9> or a std::array<std::array<float, 3>, 3> However when defining the struct like this with the following constructors: struct Matrix3x3 { Matrix3x3(std::array<float, 9> components) { } Matrix3x3(std::array<std::array<float, 3>, 3> ..

Read more

See this example: https://godbolt.org/z/5PqYWP How come this array of pairs can’t be initialized in the same way as a vector of pairs? #include <vector> #include <array> int main() { std::vector<std::pair<int,int>> v{{1,2},{3,4},{5,6}}; // succeeds std::array <std::pair<int,int>, 3> a{{1,2},{3,4},{5,6}}; // fails to compile } Source: Windows Que..

Read more

I was reading on template deduction then tried to create one. as follows:- #include <iostream> #include <array> using namespace std; template<int i,typename A,typename… B> struct deducted{ }; //Deduction guide template<typename A,typename… B> deducted(A a,B… b)->deducted<0,A,B…>; // int main(){ deducted a={"","",""}; deducted b={0,0,0}; array c={0,0,0}; } what supprises me is the test fails with error: too ..

Read more