Old-style casts should be avoided for a number of reasons. And gcc offers the handy -Wold-style-casts
to detect them. This works fine, but for non-class types, constructor-style casts behave exactly like old-style casts, minus the warning.
Example:
struct T {};
int main() {
int const* a = nullptr;
using Tp = T*; // float*, unsigned*, pick whatever
Tp b = Tp(a); // <---- hidden old-style cast: (Tp)a
*b = T{}; // ub
}
This program compiles without problems, and even exhibits UB. The constructor-syntax would have been fine if Tp had been a class type and had a constructor that accepted a
's type.
I'm trying to detect only the cases where the syntax would hide an old-style cast in addition to what -Wold-style-cast
does.