#make_from_tuple for aggregate initialization

53 messages ยท Page 1 of 1 (latest)

misty compass
#

I got my classic tuple std::tuple<std::string, int, float> and I got a cool ๐Ÿ˜Ž struct:

struct MyStruct {
  std::string a;
  int b;
  float c;
};

I want to be able to create MyStruct from my tuple (something like MyStruct { std::get<0>(tuple), ... }). I thought I could use something like std::make_from_tuple<MyStruct> but since I don't have a constructor it didn't quite work out.

Can I ask std::make_from_tuple to use aggregate initialization or something? I'd like this to work for any tuple std::tuple<...> and similar struct T.

waxen sigilBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

warm mauve
#

no, make_from_tuple is explicitly specified to create arguments with a paren-like syntax
in 20 aggregates can be initialized with that syntax

#

before that, you'd have to roll your own implementation, which is fairly straightforward I guess

misty compass
#

I just have something like

MyStruct z(std::tuple<std::string, int, float> test) {
    return std::make_from_tuple<MyStruct>(std::move(test));
}

which gives me

In file included from /Users/desgroup/Projects/kara-crimson/parser/src/literals.cpp:1:
In file included from /Users/desgroup/Projects/kara-crimson/parser/include/parser/literals.h:3:
In file included from /Users/desgroup/Projects/kara-crimson/crimson/include/crimson/crimson.h:3:
/Applications/Xcode Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/usr/include/c++/v1/tuple:1555:5: error: no matching constructor for initialization of 'MyStruct'
    _Tp(_VSTD::get<_Idx>(_VSTD::forward<_Tuple>(__t))...)
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/usr/include/c++/v1/tuple:1530:56: note: expanded from macro '_LIBCPP_NOEXCEPT_RETURN'
#define _LIBCPP_NOEXCEPT_RETURN(...) noexcept(noexcept(__VA_ARGS__)) { return __VA_ARGS__; }
                                                       ^~~~~~~~~~~
/Applications/Xcode Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/usr/include/c++/v1/tuple:1562:12: note: in instantiation of exception specification for '__make_from_tuple_impl<MyStruct, std::tuple<std::string, int, float>, 0UL, 1UL, 2UL>' requested here
    _VSTD::__make_from_tuple_impl<_Tp>(_VSTD::forward<_Tuple>(__t),
           ^
/Applications/Xcode Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/usr/include/c++/v1/tuple:1560:15: note: in instantiation of exception specification for 'make_from_tuple<MyStruct, std::tuple<std::string, int, float>>' requested here
constexpr _Tp make_from_tuple(_Tuple&& __t)
              ^
warm mauve
#

looks like mac/osx, so probably clang

#

clang + c++20 isn't a combination you ought to trust in

misty compass
#

argh

warm mauve
#

well granted I haven't followed clang dev

misty compass
#

clang has yet to implement this???

#

im in extreme pain

warm mauve
#

I'll let you guess who is the red rectangle

misty compass
#

AppleClang?

#

for the love of god

#

or i guess regular clang

#

i was kinda hoping on the right was clang 10

warm mauve
#

it's gcc-clang-msvc in order

#

haven't checked apple clang specifically, maybe it's on the list

#

apple clang is the red box that's not in frame but visible

#

but overall apple clang has even fewer stuff than clang so yeah

misty compass
#

so is the solution to break into llvm HQ and force contributors to fix this

#

grabbing my ski mask

warm mauve
#

well excluding the usual "what are you doing this and is there a better way" dance, just roll your own implementation like about everything else

misty compass
#

okay, I'll take a look at how make_from_tuple is implemented in stdlib

#

as for what im doing

warm mauve
#

it's like 1 line of code and 5 lines of declaration

misty compass
#

well that's good to hear

warm mauve
#

so I guess 8 lines

misty compass
#

I pass something a lambda that takes a tuple

#

and I found often I do a lot of lambdas like

#
thing.map([](auto tuple) {
  auto &[x, y, z] = tuple;

  return Pizza { std::move(x), std::move(y), std::move(z) };
});
#

and I just write like

#

20 of these closures

#

for different types like

#

PizzaWithPerpperoni, SteakSandwhich,

#

and basically I'm fed up and I want some .make<Pizza>() to make this lambda for me

warm mauve
#

from my pov it's kinda weird to have that many tuples

#

but yeah, that's what make from tuple does

#

mostly, I guess, it's not a lambda

#

so you'd wrap that in a lambda arguably

#

or whatever it is that is your real use/code

#

you'll probably have to figure out whatever typo or edge case I forgot about

#

I guess I forgot to include type_traits to guarantee is_aggregate_v would be usable

#

@misty compass ^

#

meh, I forgot to test on clang as well, but whatever

misty compass
#

kinda unfortunate this has to be done at all i guess :(

#

but as long as there's not performance tradeoff or anything i can rest easy

fringe perch
waxen sigilBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.