Electronicdesign 7630 122451promo1

Bjarne Stroustrup Talks About C++14

Aug. 18, 2014
Bjarne Stroustrup talks about C++ before his Q&A at the Google+ C++ Community. We had a chance to ask a few questions first.

Bjarne Stroustrup is the well-known creator of C++. He also joined the Electronic Design Engineering Hall of Fame in 2013 (see “Gallery: 2013 Engineering Hall Of Fame”). He will be holding a Q&A on August 20th at the Google+ C++ Community but I thought we could get in a couple questions early.

Wong: Your latest book is Programming: Principles and Practice Using C++, 2nd Edition. What's new in this edition?

Stroustrup: The basic approach is the same, but I have upgraded the book to use (and teach) a bit of C++11 and C++14. This allows me to simplify many of the examples and some of the text.

For example, in the first edition I use

for (int i=0; i

In the second, I immediately simplify that to

for (int x : v)
  cout << x << ′\n′;

The first version may be more familiar to C and C++ programmers, but the second is simpler. In the second editions, I can also use initialization of containers

vector v = {5,7,9,4,6,8};

The new language and library features allow me to make the examples executable earlier on and with less “boilerplate.” Basically, in the 2nd edition I can use my ideas of how to teach programming more effectively. Note that it is a book about Programming ... using C++ not a C++ language primer, though it can also be used as that. My aim is – as ever – to teach generally useful software development concepts and techniques, rather than “mere syntax.”

Wong: What are some of the new language features in C++14 and why were they added?

Stroustrup: C++14 is simply the completion of the work that became C++11. When you finish a huge project, there are things you know need to be done, but you can’t add them if you want to ship on time. Inevitably, there are refinements that you don’t realize you need until you can try the whole “product” for real. C++14 adds such features and refinements to C++11. More dramatic improvements are targeted for C++17.

So, compared to the C++11 improvements (type-safe concurrency, lock-free programming, uniform initialization, lambdas, range-for loops, regular expressions, random numbers, etc.), the C++14 improvements are deliberately tiny, but add significant convenience for users. For example,

  • We can use units for many standard library types
    auto d = 2h+5s+234ms; // d is a duration of 2 hours, 5 seconds, and 234 milliseconds
  • We can move objects into a lambda, rather than just copying
  • We can have separators in numeric literals; the single quote is the separator
    uint16_t x = 0xDEAD’BEEF; // hexadecimal DEADBEEF 
    int price = 1’200’500; // decimal 1200500 
    auto pattern = 0b1110’1101’1011’0111; // binary 1110110110110111
  • We can make an object and return a unique_ptr to it
    auto p = make_unique(“simple”,123); // p point to an Entry{“simple”,123}
  • We can use loops in constexpr functions (functions that can be evaluated at compile time)

This has already been shipping in the major C++ implementations for months!

In addition, this year and the next will see a stream of Technical Specification, which are not part ot the international standard but is guidance to implementers and will be essentially universally available:

  • File System: Provides file names, paths, and directories (portably)
  • Concepts: Provides explicit specification of template argument requirements, offering a much simpler syntax for templates, point-of-use checking, and overloading based on template argument properties

Both are the result of years of work and both are available now. I am particularly proud of “concepts” because they will dramatically change the way people think about and use generic programming

Wong: Are there any standard library changes with C++14?

Stroustrup: A few; these are again deliberately minor additions to complete the C++11 standard library, such as shared mutexes and make_unique. But note the technical specifications. More library TSs are coming, including:

    More foundational types (e.g., optional and string_vew)
  • Networking
  • Parallel algorithms
  • SIMD vectors
  • Transactional memory
  • Wong: You have probably spent as much time writing and teaching about C++ and object oriented programming as writing code and developing C++. What have you seen as the challenges to learning C++ and using it effectively?

    Stroustrup: People seem to be hell bent on doing clever and complicated stuff. Probably the hardest problem is to wean them of that. In their dash to write “sophisticated and efficient” code, many make such a mess that it becomes harder to introduce more elegant, efficient, and maintainable styles of code.

    The more code I see, the more I appreciate simplicity. Typically, simplicity is the result of deep insight.

    Wong: What do you think of contract programming like that found in Ada 2012, D and Eiffel? Is it applicable to C++?

    Stroustrup: It is, but – as usual – we have to be careful not just about what we can add, but how. You can’t just graft a feature from one language into another. The notion of invariants that underlies “contract programming” is old (going back to Peter Naur’s work in the early 1970s) and has been used in various forms in C++ for ages without rising to become a first-class, directly supported language feature.

    Constructors and destructors is the original support for that in C++: a constructor establishes the execution environment for operations on an object (partly by acquiring the resources needed) and the destructor reverses that action (in particular, the destructor releases all resources held at the point of destruction). Today, we would say “a constructor establishes the class invariant.”

    Every year or so, a new proposal to add some form of “contract programming” to C++ appears and maybe someday one that’s good enough will emerge. For now, we keep improving the ability to specify interfaces. “Concepts” (now in the process of becoming a technical specification and already shipping as a GCC branch) allow us to directly specify arguments for template arguments and similarly the C++11 static_assert allows us to express requirements on our code that can be checked at compile-time.

    Wong: What C++ tips might you have for embedded developers especially those that think C is necessary for dealing with hardware?

    Stroustrup: Measure! The idea that low-level messes of code are efficient is a stubborn myth. Learn the higher-level C+ features, measure, and measure again when necessary. Maintain proper interfaces and define clear invariants for calls across such interfaces. Templates and inlining are incredibly effective mechanisms for expressing high-level notions and generating efficient code from them.

    And, of course (?), huge hierarchies with loads of virtual functions can be damaging to performance and hard to maintain. I’m a great fan of pure abstract classes as interfaces where you can afford an indirect function call.

    Wong: Do you still get a chance to write C++ applications, possibly for your own use?

    Stroustrup: There isn’t a day where I don’t write or think about code. I write a lot of small code fragments for testing and I am involved in a range of real-world projects. Getting to do that was one of my reasons to (partially) leave academia to go back to industry. I don’t get to write much code “for my own use.” Anyway, that could be seen as too easy and too self-indulgent anyway.

    Wong: Where can we find your books?

    Stroustrup: You can find them at InformIT and in most technical bookstores (but sadly, there are so few good ones left these years). They are

    • A Tour of C++ (2014), aka Tour++, is a quick (about 180 pages) tutorial overview of all of standard C++ (language and standard library) at a moderately high level for experienced programmers.
    • The C++ Programming Language (Fourth Edition) (2013), aka TC++PL4, describes the C++11 revision of ISO C++. In particular, TC++PL4 reflects the massive increased of the standard library and the support for concurrency. TC++PL4 aims at completeness in is description of the C++ language features, standard library components, and the programming and design techniques they support.
    • Programming: Principles and Practice Using C++ (second edition) (2104), aka PPP, is an introduction to programming for people who has never programmed before. It is also useful for people who have programmed a bit and want to improve their style and technique. It is designed for classroom use, but written with an eye on self-study.
    • The Design and Evolution of C++ (1994), aka D&E, discusses why C++ is the way it is. It describes the design of C++. The emphasis is on the overall design goals, practical constraints, and people that shaped C++. A “golden oldie.”

Sponsored Recommendations

TTI Transportation Resource Center

April 8, 2024
From sensors to vehicle electrification, from design to production, on-board and off-board a TTI Transportation Specialist will help you keep moving into the future. TTI has been...

Cornell Dubilier: Push EV Charging to Higher Productivity and Lower Recharge Times

April 8, 2024
Optimized for high efficiency power inverter/converter level 3 EV charging systems, CDE capacitors offer high capacitance values, low inductance (< 5 nH), high ripple current ...

TTI Hybrid & Electric Vehicles Line Card

April 8, 2024
Components for Infrastructure, Connectivity and On-board Systems TTI stocks the premier electrical components that hybrid and electric vehicle manufacturers and suppliers need...

Bourns: Automotive-Grade Components for the Rough Road Ahead

April 8, 2024
The electronics needed for transportation today is getting increasingly more demanding and sophisticated, requiring not only high quality components but those that interface well...


To join the conversation, and become an exclusive member of Electronic Design, create an account today!