vendredi 31 janvier 2020

Run-time indexing of tuple

Suppose I have a variable constructors, which is a tuple of constructor functions represented in variadic generic lambdas.

// types for constructors 
using type_tuple = std::tuple<ClassA, ClassB, ClassC>;

// Get a tuple of constructors(variadic generic lambda) of types in type_tuple
auto constructors = execute_all_t<type_tuple>(get_construct());

// For definitions of execute_all_t and get_construct, see link at the bottom.

I can instantiate an object with:

// Create an object using the constructors, where 0 is index of ClassA in the tuple.
ClassA a = std::get<0>(constructors)(/*arguments for any constructor of ClassA*/);

Is it possible to index the type in runtime with a magic_get like below?

ClassA a = magic_get(constructors, 0)(/*arguments for any constructor of ClassA*/);

A minimal reproducible example: Try it online!

Aucun commentaire:

Enregistrer un commentaire