5

I have a type trait and concept which checks if an std::variant can hold a given type T. Now I have a type variant2 which derives from std::variant, and I want to use that type trait with the new variant2 type. How can I accomplish this elegantly?

This doesn't work (Demo):

#include <variant>
#include <string>
#include <iostream>
#include <concepts>
#include <type_traits>

template<typename T, typename Variant>
struct variant_type;

template<typename T, typename... Args>
struct variant_type<T, std::variant<Args...>> 
    : public std::disjunction<std::is_same<T, Args>...> {};

template<typename T, typename Variant>
concept is_variant_type = variant_type<T, Variant>::value;

template <typename... Ts>
struct variant2 : public std::variant<Ts...> {
};

variant2<std::monostate, int, bool, std::string> var;

int main() {
    using T = std::string;

    if constexpr (is_variant_type<T, decltype(var)>) {
        std::cout << "Worked!" << std::endl;
    }
}

"Worked" never appears on the screen which is obvious because the default type trait is SFINAED.

2 Answers 2

5

You can just fix your specialization by expressing that your template must inherit from std::variant

#include <concepts>
#include <iostream>
#include <string>
#include <type_traits>
#include <variant>

template <typename T, typename Variant>
struct variant_type;

template <typename T, template <typename...> typename Var, typename... Args>
struct variant_type<T, Var<Args...>>
    : public std::conjunction<
          std::disjunction<std::is_same<T, Args>...>,
          std::is_base_of<std::variant<Args...>, Var<Args...>>> {};

template <typename T, typename Variant>
concept is_variant_type = variant_type<T, Variant>::value;

template <typename... Ts>
struct variant2 : public std::variant<Ts...> {};

variant2<std::monostate, int, bool, std::string> var;

int main() {
    using T = std::string;

    if constexpr (is_variant_type<T, decltype(var)>) {
        std::cout << "Worked!" << std::endl;
    }
}

I just added a conjunction to achieve that and we'are done.
Live
[EDIT] sorry, it's just a bit more complicated: I added a template template parameter that implies that your inherited class has exactly the same template parameters than the base one, which might be restrictive.

5
  • I think that this answer is more generic as it make no assumption about the inherited class template parameters, it merely express that it is convertible to a variant and will retrieve its parameters.<br> Perhaps that an improvement, would be to mix both answer and enforcing that the variant2 class is indeed inherited from std::variant thus excluding class that would be implicitly convertible to your std::variant (yet I don't know exactly to do that without knowing Args...).
    – Oersted
    Jun 12 at 15:21
  • it would be very easy to adjust it, by using std::is_convertible instead of std::is_base_of, and I would also find that solution preferable. I'd also change the names of variant_type and is_variant_type to is_variant_type and is_variant_type_v respectively, to follow the standard trait naming conventions. Jun 12 at 16:13
  • @JanSchultke What name then would you suggest for the concept?
    – glades
    Jun 13 at 5:01
  • @glades concepts in the standard library usually don't have any suffix and also don't start with is_, so the concept should just be variant_type, or perhaps variant_type_in. Jun 13 at 10:05
  • @JanSchultke Indeed, I got it twisted, thanks for pointing that out :)
    – glades
    Jun 13 at 13:49
2

Since variant2 inherits std::variant, you can extract its base variant type through the following helper function

template<typename... Args>
auto as_variant(const std::variant<Args...>&) -> std::variant<Args...>;

template<typename T>
struct to_variant;

template<typename T>
  requires requires (const T& t) { as_variant(t); }
struct to_variant<T> {
  using type = decltype(as_variant(std::declval<const T&>()));
};

Then slightly modify the is_variant_type concept

template<typename T, typename Variant>
concept is_variant_type = 
  variant_type<T, typename to_variant<Variant>::type>::value;

Or more simply, just define is_variant_type directly as

template<typename T, typename Variant>
concept is_variant_type = requires (const Variant& var) {
  []<typename... Args>(const std::variant<Args...>&) 
    requires (std::same_as<T, Args> || ...)
  { }(var);
};

Demo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.