4 ms·
I have already replaced C++. C++14 has already replaced C++98 for me.
by mempko 12y ago
I have already replaced C++. C++14 has already replaced C++98 for me.
- pjc50 12y agoCan you ban "native"/bare pointers in C++14? And C-style casts?
- plorkyeran 12y ago-Werror=old-style-cast will make C casts an error in gcc and clang.
- nly 12y agoand -Wconversion and -Wsign-conversion coupled with the new C++11 {braced} initialisers deal with a lot of bad implicit conversions inherited from C
- detrino 12y agoUnfortunately this can be problematic if you include any library headers that use old-style-casts.
- plorkyeran 12y agoUsing -isystem rather than -I makes gcc/clang not emit warnings for headers found in those paths, which does extend to -Werror.
- detrino 12y agoThanks for the tip.
- cannam 12y agoThe one time I still use C casts in C++ is when comparing an int against the size of a container: if (i < (int)container.size()) This is because container.size() returns an unsigned type, and I try to avoid unsigneds in everyday code, and I like to use -Wall -Werror which, amongst other things, produces errors for signed/unsigned comparison warnings. I don't want to have to make the variable "i" unsigned, because using unsigneds for anything other than bit arithmetic is quite hazardous in practice (http://soundsoftware.ac.uk/c-pitfall-unsigned http://soundsoftware.ac.uk/c-pitfall-unsigned). How do others resolve this problem?
- mappu 12y agoYou've traded one hazard for another there, now you have a bug at runtime if the container size is between the range of signed int and unsigned int. Hope that container size isn't user controlled. I'm guilty of a few C-style casts, but certainly prefer catching things at compile time than runtime. So +1 unsigned types for me. At least the compiler helps with catching signedness issues.
- cannam 12y agoDoes the compiler help? $ cat unsigned.cpp #include <iostream> void f(unsigned int x) { std::cout << "x = " << x << std::endl; } int main() { f(-1); } $ g++ -Wall -Wextra -Wconversion -Werror unsigned.cpp $ ./a.out x = 4294967295 No error, no warning. The two are not really distinct types. See the link I posted above for more argumentation along these lines.
- shin_lao 12y agoYou should use static_cast<>() and not C style cast. You should not care about signed/unsigned, use auto.
- cannam 12y agoI would prefer to use auto when iterating through the container, certainly. But much of the code I write is numerical stuff and "i" really is logically an integer rather than an iterator, arising as the result of some actual arithmetic. I find I have to do this quite often, and I'm not going to write static_cast<int>() every time. Yes, I can see why this isn't a good idea -- I'm pained by it myself. I'm just wondering what I might have overlooked among the possibilities available for this kind of code.
- shin_lao 12y agoIf you are writing numerical stuff, you should be using more explicit types such as std::uint32_t.
- 12y ago