File tree Expand file tree Collapse file tree 1 file changed +4
-4
lines changed Expand file tree Collapse file tree 1 file changed +4
-4
lines changed Original file line number Diff line number Diff line change 2877
2877
in storage occupied by an existing object of the same type,
2878
2878
a pointer to the original object
2879
2879
can be used to refer to the new object
2880
- unless the type contains \tcode { const} or reference members ;
2880
+ unless its complete object is a const object or it is a base class subobject ;
2881
2881
in the latter cases,
2882
2882
this function can be used to obtain a usable pointer to the new object.
2883
2883
See~\ref {basic.life }.
2886
2886
\pnum
2887
2887
\begin {example }
2888
2888
\begin {codeblock }
2889
- struct X { const int n; };
2890
- X *p = new X{3};
2889
+ struct X { int n; };
2890
+ const X *p = new const X{3};
2891
2891
const int a = p->n;
2892
- new (p) X{5}; // \tcode {p} does not point to new object\iref {basic.life } because \tcode {X::n} is \tcode {const}
2892
+ new (const_cast<X*>(p)) const X{5}; // \tcode {p} does not point to new object\iref {basic.life } because its type is \tcode {const}
2893
2893
const int b = p->n; // undefined behavior
2894
2894
const int c = std::launder(p)->n; // OK
2895
2895
\end {codeblock }
You can’t perform that action at this time.
0 commit comments