问题描述
为什么这个静态断言触发?
Why does this static assertion fire?
static_assert(std::is_const<const int&>::value, "Pain");
要获得一个句法(为什么实现这样做)和语义推理为什么他们会设计这种类型的trait的接口来做到这一点)。
Would be awesome to get both a syntactic (why the implementation would do this) and a semantic reasoning (why they would have designed this type trait's interface to do this).
我知道可以抛出一个 std :: remove_reference
调用获得预期结果,但我不知道为什么这是必要的。
I am aware it is possible to throw in a std::remove_reference
call to get the expected outcome, but I'm not sure why that's necessary.
推荐答案
const int&
是对 const int
的引用。所以引用本身不是 const
。
const int&
is a reference to const int
. So the reference itself isn't const
.
这有点混乱,因此我要提出一个类比与 const int *
。它是指向 const int
的指针。但你可以修改它
It's slightly confusing, so I'm going to present an analogy with const int*
. It's the pointer to const int
. But you can modify it
const int a = 5, b = 7;
const int* ptr = &a;
ptr = &b; // pointer is modified
因此指针不是 const
。 (const指针将 int * const
)
so the pointer isn't const
. (the const pointer would be int* const
instead)
这篇关于is_const< const int&> :: value is false - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!