This question already has answers here:
Why does std::is_const<const int&>::value evaluate to false?
(2 answers)
Closed 7 years ago.
Why does this static assertion fire?
static_assert(std::is_const<const int&>::value, "Pain");
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).
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& is a reference to const int. So the reference itself isn't const.
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
so the pointer isn't const. (the const pointer would be int* const instead)
Related
This question already has answers here:
What is the difference between const int*, const int * const, and int const *?
(23 answers)
Constant pointer vs Pointer to constant [duplicate]
(8 answers)
Closed 1 year ago.
Why I have the error for the following code:
const int r = 3;
int *const ptr = &r;
However it works normaly if I define r as a plain int. As I understand, the second line only defines the pointer ptr as a const, which means that the value of this pointer cannot be changed. But why I a const pointer cannot point to a const int?
The clockwise/spiral rule says that the definition
int *const ptr = &r;
makes ptr a constant pointer to non-constant int.
That is, while the variable ptr itself can't be modified (you can't assign to ptr), what it points to can. And that doesn't match the type of r which is constant.
If you want a constant pointer to a constant int you need:
int const* const ptr = &r;
Now neither the variable ptr can be modified, nor the data it points to.
This question already has answers here:
What is the difference between const int*, const int * const, and int const *?
(23 answers)
Closed 5 years ago.
int p=10;
const int * ptr=&p; // expression 1
As far as i understood by expression 1 that the data which is pointed by pointer ptr is constant
so if i write
*ptr=10;
which is invalid ,
but if i take another pointer variable like
int * pr=&p;
*pr=19;
cout<<*ptr;
will give me the ouput 19
so now the data pointed by ptr changed
but earlier we have seen that data pointed by ptr is constant
why data is changed by another pointer variable?
const int * ptr=&p; means the data pointed to by ptr is const, but only relative to that pointer.
The pointed-to data is not necessarily really const (=originally declared const) and if it isn't, non-const pointers to it (including the original const-pointer cast to its non-const version) may change it.
If some data is really const, attempts to modify it through an non-const pointer result in undefined behavior.
This is very basic, so my suggestion is to read a basic C++ book.
Despite of that I'll provide the answer.
int p = 10;
It is a statement which declares and defines a variable named p of type int.
The content of this variable can be modified. That's because the variable p is not const.
Obviously the later statement p = 13; is still valid and it assigns a new value to that variable.
Now you have this:
const int* ptr = &p;
You're defining a pointer, named ptr which points to that variable.
Adding the qualifier const to the pointer it simply means that you cannot modify the content of the variable by means of the access of the pointer itself.
In other words, the pointer can be only used (for example) for reading the value of p.
On the other hand:
int* pr = &p;
defines a pointer which is not more const qualified.
Indeed, you can access and modify the content of the variable p by means of the usage of that pointer itself. (*pr = 19; is a valid statement).
A little bit far...
This is the general idea behind behind a "more complex world".
The statement:
const int* ptr = &p;
it's possible because the a variable can be implicitly converted in its const version.
This question already has answers here:
Purpose of returning by const value? [duplicate]
(4 answers)
Closed 6 years ago.
I think I haven't grasped the proper idea behind returning something that is const.
If I'm having a function which returns const value, doesn't it means that I cannot change the value after I returned it?
Why the compiler allows me to cast my const to a non-const variable?
Or it works only with const pointers?
const int foo(int index) {
return ++index;
}
int main(){
int i = foo(1); // why can I do this?
++i;
return 0;
}
You're doing the equivalent of this:
const int a = 42; // a cannot be modified
int b = a; // b is a copy of a...
++b; // and it can be modified
In other words, you are making a copy of a const object, and modifying said copy.
Note that returning a const value has limited, ehm, value. For built-in types it isn't important. For user-defined types, it can prevent modifying a "temporary" object, at the cost of preventing move semantics. Since C++11 it has become advisable to not return const values.
This question already has answers here:
What is the difference between const int*, const int * const, and int const *?
(23 answers)
Closed 6 years ago.
This is what I have:
const int x = 10;
int *ptr = &x; //error: 'initializing': cannot convert from 'const int *' to 'int *'
I know x can't be modified after initializing and I know how to fix this compilation problem, but why can't I use a pointer (in this case ptr) to point to x?
Although it is a const int, I don't understand the reason behind of why can't I point to it, as I can still use *ptr to represent x, right?
Because ptr is of type int*, i.e. it points to a non-const int. This allows you to modify ptr using *ptr. To prevent the const variable from being modified, the compiler prevents the initial assignment to a non-const pointer.
Making ptr point to a const int will naturally fix the problem: const int* ptr = &x;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between const int*, const int * const, and int const *?
I am trying to understand how const works in a function parameter. What is the difference between void function(int const *&i); and void function(const int *&i);? And I would appreciate if you gave an example of their implementation and of the usage of i.
They are the same, just read it from right to left:
First example: i is a reference to a pointer of a constant integer.
Second example: i is a reference to a pointer of an integer constant.
The placement of const only changes things on either side of the pointer. For example:
const int* i
means: i is a pointer to a constant integer
whereas int* const i
means: i is a constant pointer to an integer.
Those declarations are equivalent. For more info, see this page.
Best place for const correctness is the - C++ FAQ Lite
They're both the same.
It means a reference to a pointer to a const int.