简体   繁体   中英

Storing different std::tuple types in an array, vector or?

Is there any way in the latest C++ version (17 or 20 I guess) to store different kinds of tuples in a std::array or std::vector or other built-in data type? For example maybe something like ->

struct bunchOfTuples {
    std::tuple<float, float, int, int> firstTup = std::make_tuple(float, float, int, int);
    std::tuple<float, float, int> secondTup = std::make_tuple(float, float, int); };
    // bunch more tuples of differing types
};

Is there some way using templates to achieve something similar? Or a class or struct implementation? Or another way I haven't thought of I am kind of new to the latest C++ stuff? I am working on an automatic method invocation project. I have something that basically works but this would make it cleaner.

Arrays, vectors and in fact all standard containers are homogeneous. This means that all elements of the container have the same type. Different instances of the std::tuple template are different classes. As such, they cannot be stored in one array.

C++ does have heterogeneous wrappers: std::variant and std::any . Variant is an implementation of a tagged union. It can be used to store an object from a pre-defined list of types. any is a type safe wrapper for void* . it can store an object of any type. So, a container of variant or any might be useful to you.

They are only low level tools though, and for more featured heterogeneous processing, there are existing libraries such as Boost.Hana.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM