Allow ONLY c++ 11 coding features and style [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to enforce the use of modern c++ 11 features from the compiler?
We have to start a new project and stay in C++ will benefit us from the use of the our existing tools and libraries, but what is currently hurting us to the death is the abuse of C style coding, raw pointers, C style vectors and all the dangerous C features that are so error prone or produce errors that are really hard to track, you know what i mean.
I read a note about what is bad in C++ today (c++11 and beyond) is the c++ past and the abuse of low level/old features when is not necessary.
I really would like to enforce from the compiler or with other tool the use of c++ 11 features like smart pointers, containers, reference parameters, c++ standard library and not C libraries, etc.
Thank you very much.
EDIT: I thought it is not necessary to clarify the use of code review. etc. I just ask if there is or not a tool/mechanism that filters creating features, or enforce modern safer features/coding practices. All the lose ends we will manage or figure out later. Thank you very much!

You might want to take a look at clang-modernize, it can help with some of what you're looking for. In addition running static analysis to help catch bad practices could help identify areas of your codebase in particular need of some attention / modernization. There are a number of such tools associated with clang and MSVC also has its own static analysis if you're working with Visual Studio.

The only way to ensure c++11 features like auto variable declarations, using lambda definitions, range based for loops, etc. are used preferred to older techniques are human based code reviews.
Usage of deprecated stuff, like usage of std::auto_ptr may be sorted out by warnings already.
But enforcing new way styles might become hard, without having a 2nd pair of experienced eyes looking at the code.
I could think of instrumenting an appropriate static code analysis tool, to detect old style patterns and propose the c++11 standard way to do it, but that's a way too broad topic, and depends on the static analysis tool used.

At the risk of getting downvoted to oblvion.
So you or your "expert" on C++ programming, want to ban certain practices.
Because you know they are bad.
One such practice is using raw pointers. Shame that there are C++ programmers who not only use pointers very often, but think they are a good thing thing to use. Guys like Herb Sutter, who in a recent talk ( CppCon I think ) said exactly that.
Another practice you mention is using C style vectors. Not a problem since C does not have vectors.
Those are really the only two that you mention. But you do talk about banning features that cause errors. That can easily be achieved. Instead of issuing your programmers computers. Issue them etch-a sketch's.
Other then that, I would say that your company should hire people who are real experts, not some guy who read a Herb Schildt book.
PS: I was trying to remember what your post smelled like, now I remember. It smells like one of those deign doc review, where one guy spend half the time arguing about spelling errors in the design docs.

Related

Links to official style guides [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
C++ has several types of styles: MFC, Boost, Google, etc. I would like to examine these styles and determine which one is best for my projects, but I want to read from the official style guidebook. Does anyone have an official guide that they typically use?
Here are two that I found. I bet there are more:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.html
http://www.boost.org/development/requirements.html
Note: This is NOT a discussion about which style is best... only a call for official style guides that people currently use. Please refrain from bashing other style guides that you don't like.
Side question: Is there a good tool that can examine source code and tell if it matches a given style guide?
Not a Coding Guideline per se, but I find this mighty useful: Bjarne Stroustrup's C++ Style and Technique FAQ
There's no such thing as an "official" style guide - the C++ standard is entirely silent on style. One book on the subject by two highly knowledgable C++ guys is C++ Coding Standards by Sutter & Alexandrescu.
Other answers state that there is no official style guide, which was true at the time.
But in 2015, Bjarne Stroustrup announced the C++ Core Guidelines, an open source project to build authoritative guidelines for modern C++ code, led by Stroustrup himself and published by the Standard C++ Foundation:
http://github.com/isocpp/CppCoreGuidelines
In relevance to this question, the Core Guidelines also link to other guidelines, with added comments. About the often recommended Google C++ Style Guide, they say:
Geared toward C++03 and (also) older code bases. Google experts are now actively collaborating here on helping to improve these Guidelines, and hopefully to merge efforts so these can be a modern common set they could also recommend.
I have also written some tips for good coding in c++:
http://www.ivanism.com/Articles/CodingStandards.html
The post starts with:
The goal of coding standards are to increase the business value of the code. The most obvious (and indeed most important) way to do this is to make the code robust and low defect. Equally important, but more subtle goals include reducing coder friction and maintainability. As such, standards should be kept minimal -- simple enough to actually follow, and important enough to remember.
These standards should be used when building new source files. When an existing file needs to be changed, that is an appropriate time to bring it up to standard. However, it's never a good time to edit a file merely to bring it up to standard. If it ain't broke, don't "fix it" and remember to always "Keep it Working".
You'll notice that I don't touch on the classic "Religious" points:
tabs vs. spaces
indentation style
curly brace style
etc...
Consistency within a file is important and improves readability. But allowing coders to express themselves is also important. So, if you edit a file, either conform to the religion of that file, or convert the whole file to a new, consistent format. If you convert the whole file, you are effectively taking ownership of it, so be prepared to be the go-to person, or leave it as is.
C++ doesn't have and doesn't need an official style. Many organisations impose style guides on their contributors to try to maintain some kind of corporate look and feel; some of these contain snippets of good advice, but many just force you to add strange decorations that seemed like a good idea to someone writing a completely different language in the 1980s.
The only really useful advice you'll find amongst the waffle is:
Define a consistent way to distinguish types, objects, and some kinds of function (such as accessors and factories), so you'll know to write (for example) Thing thing = GetThing(); without looking the names up.
Don't start names with underscores. This is forbidden in some circumstances, and it's simpler and more readable to not do it at all than to worry about exactly when you can.
Spare a thought for the poor chap (perhaps you) who has to read and maintain the code in a few years' time.
Keep it simple.
Use your brain.
Another style guide are the The JSF air vehicle C++ coding standards.
I use my own style, which I have written up here. Whether or not you are interested in it as a style, if you're looking at styles in general you may find my discussion on the motivation for it to be useful.
To the side question: I don't personally know of any tools that analyze the style in use, but there are tools that reformat source to a given style guide. One that comes to mind is Artistic Style.
As for the side question, what you need is a static analysis tool. An expensive and huge tool is Klocwork. I've used it at a couple of shops and it can be set up to issue warnings against style issues. I don't recommend it for single users; it is more for a corporate environment. Although they may have stripped down versions for individuals.
Remember to Google for static analysis tools.

Resources to learn C++ itself, not the basics of programming? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm something of a new, reluctant convert to C++ from a Pascal, Ruby, and PHP background. I haven't worked with Pascal since a few months of torture with "Delphi Turbo;" since then I've practically been eating and sleeping Ruby and PHP. I'm already well acquainted with object oriented programming and many various subjects.
My main trouble is that I already know the rudiments of C/C++ programming, less the actual experience of working with C++. I worked with C for long enough to realize that even without macros, etc, I didn't want to work with it. (Some of the disgust was from maintaining a CGI application in C, when accustomed to Pascal's automatic string management.) I know just enough C++ to be dangerous to myself and anyone else unwitting enough to use my "code."
I'd really like to work up to a good enough understanding of C++ to use libraries like Crypto++ and Boost without major problems or in-depth knowledge of the language's intricacies. I just need to figure out how to work with C++ without killing myself (either with C++, or with the long-standing dislike that I'm already battling).
What are effective resources that will teach me C++ without assuming that I must be retaught all basic programming? I'd rather not relearn concepts that I already know, unless the paradigm in C++ is significantly different. I'm also learning on my own time, so don't recommend me a book complex enough to need a guru to explain to me, please! And I have an effective budget of $0 for learning C++, so please keep suggestions to quality online resources or books common enough that I could likely find them at the library.
Read Effective C++ by Scott Meyers - it's a good guide for getting past the basics of C++ and showing how to write and use "correct" C++ code
The C++ FAQ Lite is a great resource.
I highly recommend Stroustrup: The C++ Programming Language (Third Edition). As the author of C++ he is the authority on the language, and the book is useful as a reference as much as it is for learning the language. It's common enough that most good general purpose libraries will have a copy. He goes into quite some depth on all the features of C++, including explanations of why certain design decisions were made in the language. Personally, I think this is the best book around for programmers to learn C++.
Once you have a good grip on core C++, David Abrahams and Aleksey Gurtovoy's book, C++ Template Metaprogramming, goes into further depth and provides many examples of how C++'s template system allows you to perform complex compile-time programming, a highly valuable skill these days. This one is a little less common but you can probably find it at a university library.
I can give you a couple of keywords you might want to research in more detail:
RAII (Is pretty much the technique that ensures you don't have to worry about memory leaks. Very convenient)
Generic Programming (The STL in particular. Experiment with iterators and the standard library algorithms, and see just how powerful these abstractions are. They're a key part of what I like about C++)
Functors (Perhaps too simple on their own, but the way they can be used instead of function pointers with the algorithms mentioned above is interesting)
And just get familiar with templates, and "mild" forms of template metaprogramming. (Traits classes, for example, and (partial) specializations.
And just keep an eye on the C++ questions here on SO. A lot of interesting topics are regularly brought up.
But the best advice is probably to keep it completely separate from C. Forget everything you learned about how to use C. It either doesn't apply in C++, or leads to inferior code that is harder to read and maintain.
It's an interesting language in its own right, and has a number of unique features. Leverage those, and it can actually be fun to work with C++. Treat it as an overengineered Java, PHP or C, and it'll just make you want to throw up.
You need to write code. A lot of code in C++. There is no substitute. You also need to read good code.
I agree with the suggestion for Scott Meyers' books though. Those are pretty good.
Part of your learning will be the leap from procedural programming to OO.
I would highly recommend the book "C++ Common Knowledge" by Stephen C. Dewhurst. Don't know if it's common enough to be found at the library (it's not at mine, but my library sucks for computer books that aren't 5-10 years out of date), but it does an excellent job of taking the complex aspects of C++ and making them easy to understand, without dumbing anything down for beginners. Definitely worth the investment.
To quote from the back of the book:
This book is for you if
You're no "dummy," and you need to get quickly up to speed in intermediate to advanced C++
You've had some experience in C++ programming, but reading intermediate and advanced C++ books is slow-going
You've had an introductory C++ course, but you've found that you still can't follow your colleagues when they're describing their C++ designs and code
You're an experienced C or Java programmer, but you don't yet have the experience to develop nuanced C++ code and designs
You're a C++ expert, and you're looking for an alternative to answering the same questions from your less-experienced colleagues over and over again
C++ Common Knowledge covers essential
but commonly misunderstood topics in
C++ programming and design while
filtering out needless complexity in
the discussion of each topic. What
remains is a clear distillation of the
essentials required for production C++
programming, presented in the author's
trademark incisive, engaging style.
Here is a link to a question with answers that should help you.
https://stackoverflow.com/questions/1227764/i-need-to-improve-my-c-skills-fast-is-this-realistically-possible/1227805#1227805
For effective stuff you can find online Cplusplus.com has a pretty good reference and information.
If you can find the book "C++ Common Knowledge" (Stephen Dewhurst) at the library or cheaply online, I would add that to the list posted on the StackOverflow link above as well as "The C++ Programming Language" (Stroustrup). Going through the questions under the C++ tag right here on SO should give you some good pointers and code examples to get you on your way.
Here's a list of good C++ books which teach you C++ rather than basics of programming.
Sit down and write some C++ code.

Should I reject C++ because it's becoming a juggernaut? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have tried to keep up with C++ since they introduced 1998 ANSI/ISO C++. I absorbed the new concepts and tried to understand them. I learned about exception handling, templates, and namespaces. I've read about the new cast mechanisms and worked with the STL library.
All of these concepts required a lot of energy. But now I am somewhat worried about the future of C++ when having a look at the new C++0x standard.
Things are getting more and more complicated. This language is becoming a monster.
I'm not sure that I want to keep up with the language anymore, since I don't do my day-to-day hacking in C++ anyway. I mostly use interpreted or bytecode languages.
So why should I bother to learn this difficult, yet exceptionally powerful, language? I can do 95% of my business with Python et al. With the remaining 5%, I can deal with plain old C++ or C without hassle.
What do you think?
Everyone uses a subset of C++. For almost all application programming in C++, whether server or client side, that subset is manageable. In my opinion, the only folks that need to stay on top of absolutely every nuance of the language are the library writers -- people implementing Boost, STL, Loki, etc.
But I would absolutely use the language that fits the task. If Python is more readable and more maintainable than C++ for your job, and you don't need what C++ offers, then certainly stick with Python.
Hear what Bruce Eckel { author of the two of the so-called best C++ books } commented on C++ a few weeks ago:
That said, I hardly ever use C++
anymore. When I do, it's either
examining legacy code, or to write
performance-critical sections,
typically as small as possible to be
called from other code (my preferred
approach is to quickly write an app in
Python, then profile it and if
necessary improve performance by
calling small portions of C++ using
Python's ctypes library).
Because I was on the C++ Standards
Committee, I saw these decisions being
made. They were all extremely
carefully considered, far more so than
many of the decisions made in Java.
However, as people have rightly
pointed out, the resulting language
was complicated and painful to use and
full of weird rules that I forget as
soon as I'm away from it for a little
while -- and I figured out those rules
from first principles while I wrote
books, not just by memorizing them.
Additionally, you should read this thread and Danny Kalev's predictions on C++.
However, the growing complexity of C++ will create pressure towards splitting the language into quasi-official dialects. We can already see this trend today; it will probably intensify in the future.
EDIT:
You should take a look at this discussion, too:
C++ - Anyone else feel like C++ is getting too complicated?
First, many features of C++0x are to make the language easier to use. More readable template compile errors, more consistent initialization syntax, support for threading, which would otherwise have to rely on platform-specific libraries and so on.
So if you do use C++, I feel learning the important parts of C++0x should be a manageable task. Remember that you don't need to learn all the new features to use the language. Some features are primarily added as an aid for library implementers, for example allowing the STL to be implemented more efficiently, but which shouldn't really affect the end-users usage of the language. And some are only really necessary in very rare cases. Ignore those parts of the language.
One of their stated goals with C++0x is to avoid it becoming harder to use.
But apart from that, do you need C++? If you do your coding in other languages, why bother keeping up with C++?
You're not forced to use every feature a language provides. I don't use setjmp/longjmp in C despite it being there. I also don't use every aspect of the Java collections.
If you think the new features will make your code delivery better (faster or higher quality or both), then use them. Otherwise ignore them.
It's useful to know at a high level what they all are, if only to get you through job interviews, but half the stuff they add to languages are unnecessary in my opinion.
I never even got around to using C++ templates before switching to Java, but I knew what they were for.
It's not always about learning the latest and greatest. Software (at least at your job) is about delivery of product. That can be done in COBOL or FORTRAN if you're proficient enough at it.
No one, except maybe Bjarne and Herb Sutter, know all of C++. As you've said it's an incredibly huge language. Expecting to be able to take the entire standard + the specific implementation details of your specific compiler or compilers is truthfully unrealistic.
But you don't need to know everything in order to use C++. Instead only learn the subset of C++ that is valuable to you and your projects. It doesn't hurt to keep expanding your knowledge but unless you're writing a C++ compiler, there's no reason to know the whole thing. Even if you accomplish it, all of the people you work with won't.
So why should I bother to learn this
difficult, yet exceptionally powerful,
language? I can do 95% of my business
with python et al. With the remaining
5%, I can deal with plain old C++ or C
without hassle.
Well, for the most part you answer your own question. There is no need for you to keep up with the bleeding edge of C++ at this time.
However, the language will keep marching on. In a few years, some of the concepts you consider a bleeding-edge waste of time today will be in common use. Someday you may find during your 5% of using "plain-old C++" that some example code or code you're collaborating on uses a construct you're not familiar with. At that point, you'll need to hit the net and brush up on the new "current" C++.
Is that going to be a problem? Of course not. You're a programmer. You keep abreast of the latest programming concepts in the context of your 95% language, which also changes over time. You will likely already be quite familiar with the concepts and need only familiarize yourself with its C++ syntax when the time comes that you must use them.
Personally I hope to continue keeping up with C++, even if my career moves more toward Java or another next-gen language. Why? I would like to say because it interests me the most and because I love the complexity and expressiveness of it all. More likely, though, is just because it was my first professional language; I consider it my "native tongue".
If it does not interest you, and does not concern your job or future job, don't bother. What's wrong with that? Nothing.
Good answers.
Computer makers compete for buyers, software competes for your disk space, and languages compete for users. They do this by trying to snag each other's features.
I'm wondering how long before we see Fortran come out with lambda expressions :-)
I am hard-pressed to find a single instance where C++0x has been made more complex than C++98. There are two things which really are complex:
Concepts.
the Memory Model
but the first one has been removed again (thankfully; standardizing unimplemented features has never worked out in C++, witness throw specifications, extern templates, auto_ptr, ...), and the second isn't really something that a modern programming language can escape. It's been externally induced by Intel & Co helpfully breaking your programs to make them run faster.
The rest is just fixing annoyances that every C++ programmer has been frequently hitting in the last decades.
As a side note: I find it ­... amusing ... to see how languages such as C# get packed with a database query language (LINQ) and C++ is objurgated as being bloated.
You don't need to know every standard that comes out by heart. It does help to know about the big picture though. The 5% that you do code in may have you reinvent the occasional wheel. Depending on how much time, importance that 5% has (think Pareto) you need to take a call.
Also, any particular reason (like legacy code dependency) why you can't move that 5% to python?
First try attending a course on c++0x and make your firm pay for that :)
Our brains can fit amazing amounts of junk-knowledge. Instead of cursing and having programmer-wtf-moments we should first learn from program users and listen to other people's opinions/knowhows. Knowledge transfers much faster that way.
My suggestion would be to learn the new keywords of c++0x ( && FTW) but not bother trying to learn the entire lib. Use python for w/e you want, possibly C# for apps, then use C++(0x) when you need to do something powerful. and ask stackoverflow & google about the new container when prototyping.
You dont need to use a select few language,

c++ - relearning [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I haven't done C++ for about three years and looking to get back and ready. What is the best way? any open source projects that I might want to look at to recall all the details and be ready for interviews?
I started reading (again) C++ Primer 5th edition but was wondering whether there's more efficient way since I did program in C++ for few years before.
Just wanted to add: Does anyone know about open source projects related to finance? (e.g. servers, fix, etc)
I was facing a similiar situation a while back, and my conclusion was - no matter how many smart books you read nothing will suit you better then practicing.
Find some tutorials or set yourself some simple goals and learn by doing.
Hope that helps
PS. A friend of mine asked me once "How do you eat an elefant? - in small pieces, one at a time"
I'd start in on a real project.
If nothing else, download an open source C++ project that's in the same realm as the jobs you want to target, and start modifying. Practice helps more than anything for being comfortable.
If you're going to focus on reading, or in addition to practice, I'd actually focus on reading books that work more on using C++ well, not necessarily learning C++. Effective C++, More Effective C++, and Effective STL are great for this - you'll learn new things while refreshing your old knowledge. You can always use the primer book as a reference to study things you've forgotten as you read about them elsewhere.
Pick something that you know very little about and attempt to make a program that works with that subject area. For example if you've never done GUI work fire up C++ and try to create a simple paint program in the GUI framework of your choice (Qt, WTL, whatever). Or if you've never worked with a database grab SQLite and create a little app that manages your time. Or better yet, combine all these areas into a larger program.
The key is to force yourself to learn how to do real, practical things and solve problems using the languages paradigms. Books are great to reinforce certain practices but they'll never replace hours of frustration trying to figure out why your pointers aren't pointing where they're supposed to.
If you are good at maths (or statistics, probability, finance, geometry) and want to recall how to build re-useable functions, classes, and templates, you are welcome to help us at mathlibcpp. I recommend it, its good learning. Building a complete library on a large subject is very educatively hard ;) I found a chance to use all OO methods I know in C++, even forced to learn more.
Regarding Fix, there is QuickFAST, a very efficient implementation of the 'Fix for streaming' protocol.
Regarding C++, I would look for something that uses Boost, as it seems to be a premiere library for C++ (QuickFAST uses it to some degree).
Check out http://www.topcoder.com. This is an amazing tool for practicing programming in many different languages (C++, Java, C, C#). You can even win money if you get good at it:)
As with anything, write many programs, (re)read the best books (such as Effective C++, already mentioned). :-)
And ledger is an open source finance program written in C++. (GnuCash is another but written in C.)
Most of KDE is written in C++, albeit with Qt's signal extensions. Probably lots of examples of good code there.

How to teach a crash course on C++? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
In a few weeks, we'll be teaching a crash course on C++ for Java programmers straight out of college. They have little or no experience yet with C or C++.
Previous editions of this course were just 1 or 2 half-day sessions and covered topics including:
new language features, e.g.
header vs. implementation
pointers and references
memory management
operator overloading
templates
the standard libraries, e.g.
the C library headers
basic iostreams
basic STL
using libraries (headers, linking)
they'll be using Linux, so
Basic Linux console commands
GCC and how to interpret its error messages
Makefiles and Autotools
basic debugger commands
any topic they ask about
During the course, each person individually writes, compiles, runs, and debugs simple programs using the newly introduced features. Is this the best way to learn?
Which topics do you consider most crucial?
Which topics should be added or removed?
Which topics just can't be covered adequately in a short time?
I can only once again point to Stroustrup and preach: Don't teach the C subset! It's important, but not for beginners! C++ is complex enough as it is and the standard library classes, especially the STL, is much more important and (at least superficially) easier to understand than the C subset of C++.
Same goes for pointers and heap memory allocation, incidentally. Of course they're important but only after having taught the STL containers.
Another important concept that new students have to get their head around is the concept of different compilation units, the One Definition Rule (because if you don't know it you won't be able to decypher error messages) and headers. This is actually quite a barrier and one that has to be breached early on.
Apart from the language features the most important thing to be taught is how to understand the C++ compiler and how to get help. Getting help (i.e. knowing how to search for the right information) in my experience is the single most important thing that has to be taught about C++.
I've had quite good experiences with this order of teaching in the past.
/EDIT: If you happen to know any German, take a look at http://madrat.net/coding/cpp/skript, part of a very short introduction used in one of my courses.
If they are coming from a Java world, they are used to garbage collection. As such, I'd probably spend a bit of time talking about smart (reference counted) pointers, and how they compare to garbage collection.
If you are going to put a lot of Java programmers straight out of college to write production code, I'd say the first thing you should be concerning is pointers and memory management.
Really, those who come directly from managed code rarely have the skills to debug pointer-related exception, let alone use it correctly, or even understands how their language/tools utilize it.
Pointers is how you think not just write code.
The framework and coding practices can be taught as tips and notes along the way.
But failing to understand pointers when writing C code is just waiting to shoot yourself in the foot, if not the head.
I would like to add that you should make sure to point out where they can find language and API references. In java, the API and language specification is at your fingertips online at java.sun.com... with C or C++, it's not quite as simple and easy to find reference documentation.
Whenever I am doing something in C or C++, that is my biggest problem... trying to find what I need. I usually turn to cplusplus.com, which usually has what I need, otherwise I google for it. If you have a set of references you use (online or in the form of books), list them and tell them what you use each reference for.
Memory management (pointers, allocation etc), basics of STL and templates (since STL uses templates). I think STL is important since one would be missing the richness of the Java SE class library in C++.
I would spend a whole day discussing how to write a good class in C++. Deitel & Deitel may help as a reference.
When are constructors called?
When are assignment operators called?
When are destructors called?
What's the point for const Foo & a_foo?
You should take some time on memory management, and especially RAII.