Is_const <const int &> :: false - why?

Why is this static assertion being triggered?

static_assert(std::is_const<const int&>::value, "Pain");

      

It would be great to get both syntactic (why the implementation would do this) and semantic reasoning (why they designed an interface of this type to do this).

I know it is possible to call a call std::remove_reference

to get the expected result, but I'm not sure why this is necessary.

+3


source to share


1 answer


const int&

is a link to const int

. Therefore the link itself is not const

.

This is a bit confusing, so I'm going to present an analogy with const int*

. This is a pointer to const int

. But you can change it



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

      

so the pointer is not const

. (a const pointer would be instead int* const

)

+5


source







All Articles