为什么std :: is_const< const int&> :: value evaluate为false? [英] Why does std::is_const<const int&>::value evaluate to false?

查看:327
本文介绍了为什么std :: is_const< const int&> :: value evaluate为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个问题的旋转如何检查对象是否为const?

This is a spin off of the question How to check if object is const or not?.

我惊讶地看到以下程序

#include <iostream>
#include <type_traits>

int main() 
{
   std::cout << std::boolalpha;
   std::cout << std::is_const<const int&>::value << "\n";
}

生成此输出


false

在上下文中,考虑 const int& ;

In what context does it make sense to think of const int& as a non-const type?

推荐答案

这可能会更容易理解与这个示例

Perhaps it'll be easier to understand with this example

std::cout << std::is_const<int const *>::value << "\n";  // pointer to const int
std::cout << std::is_const<int * const>::value << "\n";  // const pointer to int

输出:

false
true

一个指向 const int 的指针,而第二个 int * 本身是 const 。因此,它导致 true ,而前者是 false 。同样,你有一个引用 const int 。如果 int& const 有效,会导致 true

The first type is a pointer to a const int, while in the second the int * itself is const. Hence it results in true while the former is false. Similarly, what you have a reference to a const int. If int& const were valid it'd result in true.

这篇关于为什么std :: is_const&lt; const int&amp;&gt; :: value evaluate为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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