简体   繁体   中英

Get std::variant types at compile time

Suppose we have defined somewhere:

using mytype_t = std::variant<int, float>;

I want to retrieve all possible types that coud be stored by mytype_t . I have checked the reference . There is no member types defined that I can use (something like: mytype_t::value_types ).

I am trying to do something like this:

using myvectype_t = std::variant<std::vector<mytype_t::value_types>...>;

You do not need a special member for that, because the information is all right there in the type:

#include <variant>
#include <iostream>
#include <vector>
#include <type_traits>

template <typename T>
struct foo;

template <typename...V>
struct foo< std::variant<V...>> {
    using type = std::variant<  std::vector<V> ...>;
};


int main() {
    using vari = std::variant<int,double>;
    using varivect = std::variant< std::vector<int>, std::vector<double>>;
    std::cout << std::is_same<foo<vari>::type,varivect>::value;
}

Output:

1

Live example

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