为什么提振:: is_same&LT; INT常量和放大器;,提振:: add_const&LT; INT和放大器;&GT; ::值等于假的? [英] Why does boost::is_same<int const&, boost::add_const<int &>::value equal false?
问题描述
我通过C ++模板元编程的亚伯拉罕&放工作; Gurtovoy
这实际上不是在第二章,但是我尝试过,而在第一次练习中(2.10,2.0),这是混淆了我:
I'm working through "C++ Template Metaprogramming" by Abrahams & Gurtovoy" This isn't actually in chapter two but is something I tried whilst working on the first exercise (2.10, 2.0) which is confusing me:
#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。输出是'真'。
The output is 'false'. However if I remove the references, i.e. 'int const' and 'int'. The output is 'true'.
推荐答案
如果你试图用三分球一样的东西,如
If you tried the same thing with pointers, as in
boost::is_same<int const *, boost::add_const<int *>::type>::value
你会发现这也是假的,因为的boost :: add_const&LT; INT *&GT; ::类型
生成为int * const的
类型,这显然是不一样的 INT常量*
。
you'd discover that it is also false, since boost::add_const<int *>::type
generates int *const
type, which is obviously not the same as int const *
.
从本质上同样的事情发生与引用,即的boost :: add_const&LT; INT和放大器;&GT; ::类型
是生成一个尝试 INT &安培;常量
。从形式上看,键入 INT和放大器;常量
在C ++中非法 - CV-资格不能应用于引用本身。因此,的boost :: add_const
的设计是一个空操作在这种情况下,这意味着的boost :: add_const&LT; INT和放大器;计算值: :键入
生成 INT和放大器;
再次
Essentially the same thing happens with references, i.e. boost::add_const<int &>::type
is an attempt to generate int &const
. Formally, type int &const
is illegal in C++ - cv-qualification cannot be applied to the reference itself. So, boost::add_const
is designed to be a no-op in this case, meaning that boost::add_const<int &>::type
generates int &
again.
这篇关于为什么提振:: is_same&LT; INT常量和放大器;,提振:: add_const&LT; INT和放大器;&GT; ::值等于假的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!