Search code examples
c++c++17stdtype-deduction

Non-deduced context like 'boost::mpl::identity<T>::type' in standard library?


Cosider the following example I digged up here on StackOverflow

  template<typename T, typename Pred> 
  T const & clamp ( T const& val, 
    typename boost::mpl::identity<T>::type const & lo, 
    typename boost::mpl::identity<T>::type const & hi, Pred p )
  {
//    assert ( !p ( hi, lo ));    // Can't assert p ( lo, hi ) b/c they might be equal
    return p ( val, lo ) ? lo : p ( hi, val ) ? hi : val;
  } 

where typename boost::mpl::identity<T>::type prevents the compiler from deducing T based on the type of the second and the third argument. This comes very handy for me, but I cannot use the Boost Library (please do not give me a hard time on that, as it is already a hard time because of that).

The question is now is something equivallent in the standard library directly which I just cannot find?


Solution

  • C++20 will have std::type_identity. But you don't really have to wait for the standard library to have it. Its entire implementation is:

    template< class T >
    struct type_identity {
        using type = T;
    };
    
    template< class T >
    using type_identity_t = typename type_identity<T>::type;