C++ is_const<;康斯特国际酒店>;::值是假的——为什么?

C++ is_const<;康斯特国际酒店>;::值是假的——为什么?,c++,c++11,C++,C++11,为什么会触发这种静态断言 static_assert(std::is_const<const int&>::value, "Pain"); 静态断言(std::is_const::value,“Pain”); 获得语法(为什么实现会这样做)和语义推理(为什么他们会设计这种类型的trait接口来这样做)将是非常棒的 我知道可以通过调用std::remove_reference来获得预期的结果,但我不确定为什么需要这样做。const int&是对const int的引用。因此

为什么会触发这种静态断言

static_assert(std::is_const<const int&>::value, "Pain");
静态断言(std::is_const::value,“Pain”); 获得语法(为什么实现会这样做)和语义推理(为什么他们会设计这种类型的trait接口来这样做)将是非常棒的


我知道可以通过调用
std::remove_reference
来获得预期的结果,但我不确定为什么需要这样做。

const int&
是对
const int
的引用。因此,引用本身不是常量

这有点让人困惑,所以我将用
const int*
进行一个类比。它是指向
const int
的指针。但是你可以修改它

const int a = 5, b = 7;
const int* ptr = &a;
ptr = &b; // pointer is modified

因此指针不是常量。(常量指针将改为
int*const

但是引用根本无法修改(使其引用其他内容),因此,或多或少,每个引用实际上都是
const
。引用不能反弹,因此它实际上是
const
,但这与语义上的
const
不同。同意。要记住,您可以非正式地说“常量引用不是常量引用”。你需要把“const reference”理解为不是一个“const”“reference”,而是一个与这两个词的组合完全不同的词。