in Education by
I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? How? I'd want to be able to do: coord a, b, c; a = {1.1, 2.0, 0}; b = {0, -1, 3.5}; c = a + b; // c = {1.1, 1.0, 3.5} JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I wouln't recommend defining new operators for STL type, or any types that aren't in your control. I would suggest instead create your own types: struct coord { // reimplement operator[], fill, size, etc. friend constexpr auto operator+ (coord const& lhs, coord const& rhs) noexcept -> coord { // ... } private: std::array _coord; }; Or you could also shortcut your way around using operators and member function already defind in std::array: struct coord : private std::array { constexpr coord(double a, double b, double c) noexcept : array{a, b, c} {} using array::operator[]; using array::begin; using array::end; using array::fill; // other operators... friend constexpr auto operator+ (coord const& lhs, coord const& rhs) noexcept -> coord { // ... } }; Note: If you want to support structured binding and offer tuple like access to your coord class, then you must add overloads for get and specialize std::tuple_size and std::tuple_element: namespace std { template tuple_element { using type = double; }; tuple_size<::coord> : integral_constant {}; } template constexpr auto get(coord const& c) noexcept -> double const& { return c[n] } template constexpr auto get(coord& c) noexcept -> double& { return c[n]; } template constexpr auto get(coord&& c) noexcept -> double&& { return std::move(c[n]); } template constexpr auto get(coord const&& c) noexcept -> double const&& { return std::move(c[n]); } This code will allow that kind of code when you switch C++17 on: auto my_coord = coord{1.0, 2.0, 3.0}; auto [x, y, z] = my_coord;

Related questions

0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I am trying to understand the usage of private const in the class. My understanding is that private const is used to make ... (void) { int const MyExample::x = 3; std::cout...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    Maps are great to get things done easily, but they are memory hogs and suffer from caching issues. And ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Maps are great to get things done easily, but they are memory hogs and suffer from caching issues. And ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I'm developing a plugin. Take a look at the following code. string request(char post_params[]) { CURL *curl; CURLcode res; std:: ... .) { std::ofstream file ("d:/t/t.txt"); file...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    What are the data types in R on which binary operators can be applied? (a) Scalars (b) Matrices (c) ... and Debugging of R Programming Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    ________ provides needed string operators in R. (a) str (b) forecast (c) stringr (d) sqldf I got this ... Regression of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    ndarray is also known as the alias array. (a) True (b) False This question was posed to me in ... questions and answers pdf, Data Science interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    ndarray is also known as the alias array. (a) True (b) False...
asked Oct 7, 2021 in Technology by JackTerrance
0 votes
    ___________________ is alike as that of Access Point (AP) from 802.11, & the mobile operators uses it for ... -for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Nov 1, 2021 in Education by JackTerrance
...