Reputation: 119099
GCC and Clang both reject the C-style cast in the following code.
http://coliru.stacked-crooked.com/a/c6fb8797d9d96a27
struct S {
typedef const int* P;
operator P() { return nullptr; }
};
int main() {
int* p1 = const_cast<int*>(static_cast<const int*>(S{}));
int* p2 = (int*)(S{});
}
main.cpp: In function 'int main()': main.cpp:7:25: error: invalid cast from type 'S' to type 'int*' int* p2 = (int*)(S{}); main.cpp:7:15: error: cannot cast from type 'S' to pointer type 'int *' int* p2 = (int*)(S{}); ^~~~~~~~~~~
However, according to the standard, a C-style cast can perform the conversions performed by a static_cast
followed by a const_cast
. Is this code well-formed? If not, why not?
Upvotes: 27
Views: 725
Reputation: 60979
This is core issue 909:
According to 5.4 [expr.cast] paragraph 4, one possible interpretation of an old-style cast is as a
static_cast
followed by aconst_cast
. One would therefore expect that the expressions marked #1 and #2 in the following example would have the same validity and meaning:struct S { operator const int* (); }; void f(S& s) { const_cast<int*>(static_cast<const int*>(s)); // #1 (int*) s; // #2 }
However, a number of implementations issue an error on #2.
Is the intent that
(T*)x
should be interpreted as something likeconst_cast<T*>(static_cast<const volatile T*>(x))
Rationale (July, 2009):
According to the straightforward interpretation of the wording, the example should work. This appears to be just a compiler bug.
This was apparently never resolved by neither Clang nor GCC. Time to open tickets.
Upvotes: 10