简体   繁体   中英

Get type from variant at compile time

I need to do a typecheck on whether a variant type can hold a type at compile time.

I am converting an enum and a string to a variant, but I want the library to be compatible with a user provided variant (for the types they support). So I have a template parameter CustomVariant to represent a variant over a subset of the supported types, AlphaBeta , Gamma , Delta , and Epsilon . I would like to return std::nullopt if I can't create a valid variant.

template <typename CustomVariant>
std::optional<CustomVariant> AsCustomVariant(LargeEnum type, const std::string& name) {
  case LargeEnum::ALPHA:
  case LargeEnum::BETA:
    return ConvertAlphaBeta(name);

  case LargeEnum::GAMMA:
    return ConvertGamma(name);

  case LargeEnum::DELTA:
    return ConvertDelta(name);

  case LargeEnum::EPSILON:
    return ConvertEpsilon(name);

  default:
    return std::nullopt;
}

The idea is to use some sort of template magic that can do something like:

if (std::type_can_convert<CustomVariant, Gamma>) {
  return ConvertGamma(name);
} else {
  return std::nullopt;
}

With c++17 (I know it's tagged with c++11), this is super easy - you don't even have to really do anything:

#include <variant>
#include <type_traits>
#include <string>

using namespace std;

int main() {

    // this works, as expected
    if constexpr(is_constructible_v<variant<int>, double>) {
        // this will run
    }

    // this is fine - it just won't happen, 
    if constexpr(is_constructible_v<variant<int>, string>) {
        // this won't run
    } else {
        // this will run
    }
    // but obviously the assignment of a string into that variant doesn't work...
    variant<int> vi="asdf"s;
}

https://godbolt.org/z/I-wJU1

First I'd do this:

template<class T>struct tag_t{using type=T;};
template<class T>constexpr tag_t<T> tag{};

template<class...Ts>using one_tag_of=std::variant<tag_t<Ts>...>;

using which_type=one_tag_of<AlphaBeta, Gamma, Delta /* etc */>;

which_type GetType(LargeEnum e){
  switch (e){
    case LargeEnum::Alpha:
    case LargeEnum::Beta: return tag<AlphaBeta>;
    // etc
  }
}

Now we do this:

template <typename CustomVariant>
std::optional<CustomVariant> AsCustomVariant(LargeEnum type, const std::string& name) {
  auto which = GetType(type);
  return std::visit( [&name](auto tag)->std::optional<CustomVariant>{
    using type=typename decltype(tag)::type;
    if constexpr (std::is_convertible<CustomVariant, type>{})
      return MakeFromString( tag, name );
    return std::nullopt;
  }, which );
}

this leaves MakeFromString .

Write overloads like this:

inline Delta MakeFromString(tag_t<Delta>, std::string const& name){ return ConvertDelta(name); }

note, not specializations. Just overloads.

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