I need to select between two types for a variable at compile time. One of these types have a concept requirement which is not always satisfied and that is when I want to select the other type. I am trying to achieve this with std::conditional_t, but both types to std::conditional are evaluated causing a template constraint failure.
Here’s a minimal example:
#include <concepts>
template <typename T> requires std::same_as<T, int>
struct Foo {};
using MyType1 = std::conditional_t<true, int, Foo<int>>; // Compiles
using MyType2 = std::conditional_t<false, int, Foo<int>>; // Compiles
using MyType3 = std::conditional_t<true, int, Foo<double>>; // Doesn't compile (constraint fails)
using MyType4 = std::conditional_t<true, Foo<double>, int>; // Doesn't compile (constraint fails)
MyType3 and 4 both fails since Foo does not satisfies the constraint on Foo. For MyType3 I would expect it to work since the conditional selects int as the type, not Foo.
Can I achieve my goal with std::conditional or any other means?
user339443 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.