Reputation: 4655
The following simple programm fails to compile with MSVC but not with GCC and Clang? (i.e. MSVC triggers the static_assert
).
#include <utility>
class Element {
virtual ~Element() = default;
};
struct Proxy {
Element* e{};
};
int main() {
Proxy p;
static_assert(std::is_same<decltype(dynamic_cast<const Element*>(p.e)), const Element*>::value, "not const???" );
}
I think the standard is quite clear:
The result of the expression dynamic_cast(v) is the result of converting the expression v to type T. T shall be a pointer or reference to a complete class type, or “pointer to cv void.” The dynamic_cast operator shall not cast away constness ([expr.const.cast]).
Is there some other part in the standard that supports MSVC here, or is it a compiler bug? Or even UB? But if so, why?
Live code here.
Upvotes: 3
Views: 137
Reputation: 170055
It looks like a compiler bug. [expr.dynamic.cast]/3 implies the result should be a pointer to a const-qualified class type.
If the type of v is the same as T, or it is the same as T except that the class object type in T is more cv-qualified than the class object type in v, the result is v (converted if necessary).
I emphasized "converted if necessary" because it applies to the const qualification of the class type, I believe.
Upvotes: 1