https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80544
Bug ID: 80544 Summary: result of const_cast<T* cv> should by cv-unqualified Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: redi at gcc dot gnu.org Target Milestone: --- This is valid: template<typename T> struct check { }; template<typename T> struct check<const T>; // incomplete template<typename T> check<T> f(T&&) { return {}; } int main() { int i; f(const_cast<int* const>(&i)); } Clang accepts it, EDG accepts with a helpful warning: "v.cc", line 8: warning: type qualifier is meaningless on cast type f(const_cast<int* const>(&i)); ^ G++ gives a bogus error: v.cc: In function ‘int main()’: v.cc:8:31: error: invalid use of incomplete type ‘struct check<int* const>’ f(const_cast<int* const>(&i)); ^ v.cc:1:29: note: declaration of ‘struct check<int* const>’ template<typename T> struct check { }; ^~~~~ v.cc: In instantiation of ‘check<T> f(T&&) [with T = int* const]’: v.cc:8:31: required from here v.cc:4:31: error: return type ‘struct check<int* const>’ is incomplete template<typename T> check<T> f(T&&) { return {}; } ^ The result of a const_cast to a pointer type is a prvalue, and since a pointer is not a class or array type the type of the prvalue should be cv-unqualified.