9 ms·
Being new to C++, on of my biggest gripes is that I often feel unsure whether I'm using the right way to do things, or if I'm using an old method that was repla
by turbinerneiter 9y ago
Being new to C++, on of my biggest gripes is that I often feel unsure whether I'm using the right way to do things, or if I'm using an old method that was replaced by a new one. Especially given that I never took the time to properly learn the language, but mostly piece together SO answers ... .
Is there a C++17-exclusive compiler-flag? Or linters that inform you about outdated concepts? Some way to get rid of the backwards compatible cruft when writing new things.
- aldanor 9y agoclang-tidy has a few 'modernize' type lints; it's far from comprehensive but it's something.
- kanwisher 9y agoThis is actually really troublesome, I coded a lot of c++ about 15 years ago. So initially I tend to write code thats old style. Googling samples is all over the place on what version its using. It would be nice if someone built a linter around this
- pjmlp 9y agoTrue, but it happens to any language that has been long around. I stopped following Python regularly around 2.2 timeframe and only use it occasionally as better shell scripts when on UNIX like systems. If I was to actually build an application with it, I bet my code wouldn't be very up to date with 3.7 Pythonic code.
- humanrebar 9y agoLook at the "modernize" checks in clang-tidy.
- xfer 9y agoNo, your best bet is to check cppreference.com to see when the method was introduced and learning from a recent enough book.
- pjmlp 9y agoThere is ongoing work to have linters advice on the Core Guidelines items, which are considered the best practices. https://github.com/isocpp/CppCoreGuidelines https://github.com/isocpp/CppCoreGuidelines For those that aren't aware, actually some of the input from Microsoft side was based in the work done with Midori and having System C# ideas applied to C++. VC++ with the code checkers and clang with clang-tidy. Not sure about the current state of gcc or other commercial C++ compilers.
- gumby 9y agoWe started our current codebase with a blank Emacs buffer back in February and went straight to (draft) C++17. Everything is compiled with both gcc and clang. Flags like -Wall, -Wpedantic -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-c++11-extensions are your friends. But apart from that, languages have “cultural dialects”. So for example I essentially always loop with for(;;) and so a while (or do while!) looks alien to me. Likewise, a destructuring bind is not the only way to unpack a loop variable, so no compiler would mandate its use. It’s a large system programming language so it will take a while to learn a lot of nuances...but you can get a ton of great code written without learn8ng that stuff.
- criddell 9y agoWhat's the justification for using for(;;) rather than a while loop?
- andyjohnson0 9y agoFor an infinite loop, some people prefer it because the lack of a condition means it looks simpler than while(true), and therefore signals its meaning more effectively. I'd be very surprised if there was any difference in terms of the generated code. Personally I use while(true).
- twoodfin 9y agoThere was a very interesting little side note in John Regehr's recent CppCon talk[1] on undefined behavior: Apparently, C11 added language that forbids compilers from eliding side-effect free infinite loops if they're of the form `while (<constant expression>)` but not other loop forms. C++ has not yet adopted this language, but it might in the future. A strange choice by the committee I'd be interested in reading more about. [1] https://youtu.be/v1COuU2vU_w?t=2327 https://youtu.be/v1COuU2vU_w?t=2327
- TFortunato 9y agoNot a compilers guy, so when you say "eliding loops", do you refer to optimizing out loops that the compiler assumes are side-effect free? Coming from an embedded background, the form "while (true) { // do nothing of interest }" is seen a lot in embedded code. If your code is interrupt driven, or running certain RTOSes, you would usually see this at the end of main(), since everything happens in interrupts. You might also see this in error handlers, and "unimplemented" interrupt handlers during development, as a way to trap the program without halting / resetting the system completely. If this kind of loop was optimized out / you returned-from-main on your embedded system, interesting and unintended things may happen. While I've never had problems with these particular loops being optimized out (that I can remember!), I do remember having to do other vary silly hacks to try and tell the compiler not to optimize things out, because something is happening in hardware that it wasn't aware of, so I wonder if this is just a practical way of making sure that this kind of code keeps working with newer compilers, even in the face of aggressive optimization.
- quantumhobbit 9y agoScott Meyers' Effective C++ books are really good for learning the "right way" to code C++. He has a good intro to c++11 and 14 too, https://www.goodreads.com/book/show/22800553-effective-modern-c https://www.goodreads.com/book/show/22800553-effective-moder... Don't worry about not taking the time to properly learn C++ . It is a huge huge language and trying to learn it all at once from first principles will take you forever. Just try to do things in the most memory safe way possible.
- crusso 9y agoI don't use C++ anymore - but Scott Meyers' books on C++ were the best investments I could have made back in the early 2000's. He focused on some real pain/productivity points of the language and standard library that made a huge difference in my approach to the problems I was working on.
- eco 9y agoEffective Modern C++ is a great book but it is in no way an intro to anything. It keeps with tradition of exposing the warts in C++ that you should know about but the issues covered in the newest one are very complex compared to the previous book and are not at all something that someone just learning the language or even just catching up on the newest standards should do. If you want to catch up on C++11 and parts of C++14 I recommend Stroustrup's A Tour of C++. I'd probably read the other Effective C++ books (even though they are dated now) before cracking open Effective Modern C++.
- fnord123 9y ago>Is there a C++17-exclusive compiler-flag? Or linters that inform you about outdated concepts? Some way to get rid of the backwards compatible cruft when writing new things. clang-tidy might be the closest thing you're looking for: http://clang.llvm.org/extra/clang-tidy/ http://clang.llvm.org/extra/clang-tidy/ There is another language that always gets mentioned in C++ posts and that languages has a tool called clippy which does what you want. It's dreamy.
- stepik777 9y agoThat other language doesn't have that much outdated concepts as C++
- Q6T46nT668w6i3m 9y agoThis is a solid (and likely unintentional) pun.
- tomtheengineer 9y agoCLion also has clang-tidy built in: https://blog.jetbrains.com/clion/2017/04/clion-2017-2-eap-clang-tidy/ https://blog.jetbrains.com/clion/2017/04/clion-2017-2-eap-cl...
- fnord123 9y agoLooks good. Jetbrains are doing a great job. Their vim keybindings are also complete enough for me. :)
- GuB-42 9y agoMy opinion is that there is no "right way" of doing C++, unlike, say, python. You can write a kernel in C++, or you can do high level UI, or a game engine, and these will be very different, you can almost say you are using a different language. For example, to allocate an array, you can use a vector, array, new, or malloc. Most people will tell you to use a vector, but a vector uses a bit more memory than an array, so you might want to use this instead, but you may not want to link with the STL at all, so you go with new, but if you don't want to deal with exceptions or just want a block of raw memory, you may want to go with malloc instead. Just use the new features if it makes life easier for you, that's the idea. As for linters, I know that cppcheck can warn you of C-isms (ex : C-style casts) but I don't know any of them that target modern C++ variants.
- T-zex 9y agoI'm just a hobbyist, but "new" and especially "malloc" are not "right way of doing C++". Use of array is non idiomatic either. Instead you should always aim for vector and smart pointer "factory" methods make_shared or make_unique.
- drubbo 9y agoYour parent poster was probably referring to std::array.
- AlexeyBrin 9y ago> I'm just a hobbyist, but "new" and especially "malloc" are not "right way of doing C++" How do you think the std::vector is implemented under the hood ?
- pducks32 9y agoI think what he's saying is that using `std::vector` is better than using pointers and `malloc` from the point of view of doing simple things. (Simple is not the right term but can't think of a better word). I mean 80% of times.
- smhenderson 9y ago
- Xophmeister 9y agoI guess if you set the compiler's standard flag to C++14 or C++11, etc. and it compiles without warning or error, then you are not using any C++17 features. How exactly this would be useful remains to be seen!
- tapirl 9y agoAnother problem is it becomes harder and harder to understand the code written by others.