Search code examples
c++c++11templatessfinaetype-traits

SFINAE for true_type and false_type in std::conditional


What's the best way to make this compile?

// Precondition: Dims is either a pointer or std::map.

using T = std::conditional_t<std::is_pointer_v<Dims>,
    std::remove_pointer_t<Dims>,
    typename Dims::mapped_type>;

When Dims is a pointer, I am getting:

error: template argument 3 is invalid

How do I make it work in SFINAE manner, when condition is true?


Solution

  • template<class T>
    struct mapped_type{using type=typename T::mapped_type;};
    using T = typename std::conditional_t<std::is_pointer_v<Dims>,
                             std::remove_pointer<Dims>,
                             mapped_type<Dims>>::type;
    

    we defer the "execution" until after the condition.