什么是的&QUOT点;提高:: MPL ::身份< T> ::类型"这里? [英] What's the point of "boost::mpl::identity<T>::type" here?

查看:168
本文介绍了什么是的&QUOT点;提高:: MPL ::身份< T> ::类型"这里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查 在升压:

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

如果我查找文档,<一个href=\"http://www.boost.org/doc/libs/1_53_0/libs/mpl/doc/refmanual/identity.html\"><$c$c>identity返回模板参数不变。

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

身份元函数。返回X不变。

The identity metafunction. Returns X unchanged.

那么,在这里用它的意义呢?

So what's the point of using it here?

不是类型名称的boost :: MPL ::身份&LT; T&GT; ::类型等同于 T

推荐答案

一个的嵌套名称说明的创建的非上下文推断的。因此,编译器就不会试图推断类型 T 基于声明的第二和第三个参数:

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 &

键入 T 将只推导基于第一参数的类型,然后使用实例化类型的其余参数。使用身份型是一种常见的伎俩某些参数prevent模板参数类型推演,否则将导致不明确调用错误的情况下,该类型的参数不同,但使用相同类型的模板参数。它有时可以还希望不要让编译器自动推断类型,并强制的主叫的做他/她自己的。

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.

这篇关于什么是的&QUOT点;提高:: MPL ::身份&LT; T&GT; ::类型&QUOT;这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆