Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

c++ - What's the point of "boost::mpl::identity<T>::type" here?

I was checking the implementation of clamp in boost:

  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;
  } 

If I look up the documentation, identity returns the template argument unchanged.

The identity metafunction. Returns X unchanged.

So what's the point of using it here?

Isn't typename boost::mpl::identity<T>::type equivalent to T?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A nested-name-specifier creates a non-deduced context. Therefore, a compiler will not attempt to deduce type T based on the second and third parameters declared as:

typename boost::mpl::identity<T>::type const &

Type T will be deduced only based on the type of the first argument, and then used to instantiate the types of the rest parameters. Using the identity type is a common trick to prevent template argument type deduction on certain parameters, that otherwise would result in an ambiguous call error in case the types of arguments differ, but utilize the same type template parameter. It can be also sometimes desired not to let a compiler automatically infer the type, and force a caller do it on his/her own.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...