What#39;s the point of quot;boost::mpl::identitylt;Tgt;::typequot; here?(“boost::mpl::identitylt;Tgt;::type有什么意义?这里?)
问题描述
我正在检查 clamp代码>在boost中:
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;
}
如果我查看文档,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?
typename boost::mpl::identity<T>::type
不是等价于 T
吗?
推荐答案
nested-name-specifier 创建一个非推导的上下文.因此,编译器不会尝试根据声明为的第二个和第三个参数来推断类型 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 &
Type T
只会根据第一个参数的类型推导,然后用于实例化其余参数的类型.使用 identity
类型是防止对某些参数进行模板参数类型推导的常见技巧,否则会在参数类型不同但使用相同类型的模板参数的情况下导致模棱两可的调用错误.有时也可能希望不让编译器自动推断类型,而是强制调用者自己做.
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.
这篇关于“boost::mpl::identity<T>::type"有什么意义?这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“boost::mpl::identity<T>::type"有什么意义?这里?


基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01