3 ms·
For 1, I have seen somewhere that it a reason why C++ casts are so verbose compared to C casts: like "reinterpret_cast<A>(b)" vs "(A)b". Type casting in C++ may
by GuB-42 4y ago
For 1, I have seen somewhere that it a reason why C++ casts are so verbose compared to C casts: like "reinterpret_cast<A>(b)" vs "(A)b". Type casting in C++ may be the result of bad design. And while casts definitely have a place, they are ugly, and therefore, they should look ugly.
- xigoi 4y agoBut C++ still lets you use C-style casts, which kind of defeats the point.
- munch117 4y agoNot so much, you could phase out C-style casts over time. C++ compilers could grow options to warn about C-style casts (if they haven't already), shops could outlaw them in coding style guides, and slowly it would go away. What does kinda defeat the point is that C++ adds a new form of cast that is even less conspicuous than a C-style cast: typedef int* int_p; int f(char* p) { int* ip = int_p(p); // basically a reinterpret_cast return *ip; } int main(int argc, char** argv) { return f(argv[0]); } gcc -Wall compiles this with no warnings.
- scatters 4y agoUgh, I really need to clean up and submit my patch that adds those warnings. Hopefully I'll have some time this year.
- planede 4y agoYou can blanket ban C-style casts with appropriate warnings and static analysis.