为什么 boost::is_same<int const&, boost::add_const<int &>::value 等于 false?
Why does boost::is_same<int const&, boost::add_const<int &>::value equal false?
我正在学习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;
}
输出为"false"。但是,如果我删除引用,即"int const"answers"int"。输出为"true"。
如果您尝试了与中的指针相同的操作
boost::is_same<int const *, boost::add_const<int *>::type>::value
您会发现它也是false,因为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 &
。
您不能向引用添加常量,它将是int& const
,这是不可能的。
就像使用typedef一样,添加限定符不是文本替换,而是对整个类型有效的操作。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 呼叫运营商<<临时
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- 运营商&lt;&lt;无法访问班级的私人int
- std::pair的默认构造函数<>将基本类型(int等)设置为零
- 如何显示/打印字符串对象?cout & lt; & lt;Int工作,count <<字符串