Re: const char* = new char[6]
S S posted:
>
Yes, but it would have been undefined if I would have written
char const *p = new char const[6];
You are correct.
First of all, let's start off with guinea pig: a const pointer to a const
int:
int const *const p;
const int *const p; /* These two are the same */
This would only make sense if you want to yield an L-value, so I will cast
to a reference type. (Unless you cast to a reference type, a cast always
yields an R-value in C++.)
const_cast<int const*&>(p)
To yield an R-value: const_cast<int* >(p)
or,
To yield an L-value: const_cast<int* const&>(p)
(Not that I didn't write const_cast<int* constfor the first one -- reason
being that it would have been redundant because the cast yields an R-
value.)
Yield an L-value:
const_cast<int* &>(p)
--
Frederick Gotham
S S posted:
>This behaviour of this statement is well-defined, as it does not modify
>const data.
>>
>const data.
>>
Yes, but it would have been undefined if I would have written
char const *p = new char const[6];
You are correct.
Can you please give 2 syntaxes in the given context where
we strip away constness
we strip away constness
First of all, let's start off with guinea pig: a const pointer to a const
int:
int const *const p;
const int *const p; /* These two are the same */
1- for pointer itself is const
This would only make sense if you want to yield an L-value, so I will cast
to a reference type. (Unless you cast to a reference type, a cast always
yields an R-value in C++.)
const_cast<int const*&>(p)
2- data is const
To yield an R-value: const_cast<int* >(p)
or,
To yield an L-value: const_cast<int* const&>(p)
(Not that I didn't write const_cast<int* constfor the first one -- reason
being that it would have been redundant because the cast yields an R-
value.)
3- both
const_cast<int* &>(p)
--
Frederick Gotham
Comment