我正在努力通过“C++ 模板元编程”,作者:Abrahams 和 Gurtovoy”
这实际上并不在第二章中,而是我在第一个练习(2.10,2.0)中尝试过的,这让我感到困惑:
#include <iostream>
#include <boost/type_traits.hpp>
std::string display(bool b)
{
return (b ? "true" : "false");
}
int main()
{
using namespace std;
cout << display(boost::is_same<int const&, boost::add_const<int &>::type >::value) << "\n";
return 0;
}
输出为“假”。
但是,如果我删除引用,即“int const”和“int”。输出为“真”。
如果您尝试使用指针进行相同的操作,如
boost::is_same<int const *, boost::add_const<int *>::type>::value
你会发现它也是假的,因为boost::add_const<int *>::type
产生int *const
类型,这显然不一样int const *
.
本质上,引用也会发生同样的事情,即boost::add_const<int &>::type
是尝试生成int &const
。正式地,输入int &const
在 C++ 中是非法的 - cv 限定不能应用于引用本身。所以,boost::add_const
在这种情况下被设计为无操作,这意味着boost::add_const<int &>::type
产生int &
again.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)