4

In c++14 I have the following type:

std::tuple<int[2], int>;

How can I properly initialize it? This

std::tuple<int[2], int> a {{2,2},3};

gives me this error:

/usr/include/c++/5/tuple:108:25: error: array used as initializer

While this:

std::tuple<std::array<int,2>, int> a {{2,2},3};

works, but I want to be able to work with standard C-style arrays

3
  • 3
    Why do you want raw arrays? std::array<int,2> solves the problem and is a lot easier to work with. If your worried about speed or efficiency note that std::array is a zero cost abstraction and as long as you compile with optimizations on you get the same assembly code. Commented Jan 31, 2019 at 16:26
  • 5
    std::tuple is not aggregate, and int[2] is not copyable :-/ ... Commented Jan 31, 2019 at 16:26
  • @NathanOliver, I agree with you. I have to work with existing code which uses them though, so I'm trying to see if there are other ways Commented Feb 1, 2019 at 11:48

1 Answer 1

7

std::tuple is not an aggregate and doesn't provide a list-initializer constructor. This makes list initialization with that type impossible to use with C-arrays.

You can however use std::make_tuple:

auto a = std::make_tuple<int[2], int>({2,2},3);
Sign up to request clarification or add additional context in comments.

3 Comments

or std::make_tuple<int[2]>({2,2},3); :). and make_* is normally to let compiler deduce type...
@Jarod42 That's cleverly bizarre. Won't use ;)
The type turns out to be std::tuple<int*, int>, so we're not there yet unfortunately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.