为什么提振:: is_same< INT常量和放大器;,提振:: add_const< INT和放大器;> ::值等于假的? [英] Why does boost::is_same<int const&, boost::add_const<int &>::value equal false?

查看:179
本文介绍了为什么提振:: is_same< INT常量和放大器;,提振:: add_const< INT和放大器;> ::值等于假的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过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屋!

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