What's the point of “boost::mpl::identity::type” here?

后端 未结 1 1311
傲寒
傲寒 2020-12-11 02:07

I was checking the implementation of clamp in boost:

  template 
  T const & clamp ( T const& val, 
    typename boo         


        
相关标签:
1条回答
  • 2020-12-11 03:00

    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.

    0 讨论(0)
提交回复
热议问题