Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
Programming

Stroustrup Reveals What's New In C++ 11 305

snydeq writes "Bjarne Stroustrup discusses the latest version of C++, which, although not a major overhaul, offers many small upgrades to appeal to different areas of development. From the interview: 'I like the way move semantics will simplify the way we return large data structures from functions and improve the performance of standard-library types, such as string and vector. People in high-performance areas will appreciate the massive increase in the power of constant expressions (constexpr). Users of the standard library (and some GUI libraries) will probably find lambda expressions the most prominent feature. Everybody will use smaller new features, such as auto (deduce a variable's type from its initializer) and the range-for loop, to simplify code.'"
This discussion has been archived. No new comments can be posted.

Stroustrup Reveals What's New In C++ 11

Comments Filter:
  • by Godai ( 104143 ) * on Friday February 24, 2012 @03:39PM (#39152079)

    I mean, it's not, but it makes it sound like C++11 is a minor update. Lambdas, auto, concurrency, are these minor updates? There's a lot of interesting stuff in C++11!

    • The biggest addition for me is a fix finally for the smart pointer class. Seems like unique_ptr was a long time in coming.
    • by genjix ( 959457 ) on Friday February 24, 2012 @04:04PM (#39152533)

      I've been using the C++11 for 6 months now in my own project (libbitcoin [libbitcoin.org]) and the new features and syntax really make your code sharper, clearer and better. C++ is no longer that unsafe language if you know how to code in it properly - you never really have to do any manual memory management if you use shared pointers.

      constexpr allowed me to create compile time constants that are the function of a bunch of complicated expressions. Sure, I could just put the result in the code, but by using constexpr (a far more expressive metaprogramming utility than templates) I can document where those constants came from by using code. Neat huh!

      Using variadic templates, I was able to write decorators that can be applied to any function. I simply wrap my functions with this class and then its operator() will be called before calling the passed in function object (which I can define using lambdas or std::bind now :)

      auto means I no longer have to type std::vector>::iterator in every for loop. Likewise for (const transaction& tx: block.transactions) is much more terse and clearer.

      The new features to the standard library are brilliant. Threading has never been easier: std::thread t(foo, x, y); will call foo(x, y) in a new thread. When I decide to finish the threads and then join them I call: t.join(); ... Simple.

      As libbitcoin is highly asynchronous, I don't like to use exceptions (which thread does it originate in? where does it get caught? .etc). C++11 now provides the header which defines std::error_code(). An error_code object can be tested as a boolean (to see whether the error is set or not) or compared against an enum value (which you define). They also have an error message (which if you defined the enum value it is set to, you can also set the message), and also you can group different error code values into broader categories! Really useful for asynchronous programming.

      std::atomic for a thread-safe counter (useful when you have multiple thread paths executing and want to see when they all finished - increment your counter by one after each path completes) and std::atomic for a thread-safe flag. ... That's off the top of my head. There are dozens of many small things like this. C++ was always my 'native' language, but now it's truly my home.

      • by genjix ( 959457 )

        damn my angle brackets got blanked out.

        [system_error] for std::error_code

        std::atomic[size_t] for thread-safe counter
        std::atomic[bool] for thread-safe flag

      • What C++ compiler are you using?
        • by genjix ( 959457 ) on Friday February 24, 2012 @05:02PM (#39153439)

          > What C++ compiler are you using?

          g++ 4.6 - standard in Ubuntu

          Two of the features I'm waiting on are class level non-static initialisers and templated typedefs. I've heard Microsof's C++ compiler has better C++11 support but I've never tried it.

          Beware that MingW has a bug so std::thread is disabled. I've heard mingw-w64 works better. You might want to also try boost::thread (same library essentially, except std::thread has move semantics).

          • by bipbop ( 1144919 )
            std::to_string() is also disabled on MinGW. Minor, but something to be aware of.
          • VC++ 2010 used to have better C++11 support than g++ for some time, but the latter has overtaken it now, and it looks like it'll keep ahead for the next release also - VC still doesn't do variadic templates, for example. It doesn't have instance field initializers, either, nor templated typedefs.

            That said, C++11 support is being implemented pretty rapidly in general - I mean, we've only just got the final spec out, what, a couple months ago? and more than half of it is already supported in all major compile

          • Microsoft's C++11 support in VS2010 was pretty good at the time: lambdas, auto, new function decl syntax, and that's about it.
            They've really dropped the ball in VS11 though: They've basically added strongly types enums, and that's about it.

            Still no word on variadic templates, template aliases or initializer lists.

            • Slide 5 of the deck here [msdn.com] says that initializer lists, template aliases, variadic templates, and other features are coming in a series of out of band releases after VC11 RTM (but sooner than the next major release [msdn.com] of VC). That slide also lists the stdlib and language features that are included in VC11 Beta/RTM.

      • Re: (Score:3, Informative)

        The new features to the standard library are brilliant. Threading has never been easier: std::thread t(foo, x, y); will call foo(x, y) in a new thread. When I decide to finish the threads and then join them I call: t.join(); ... Simple.

        Sure. But it should be noted that this feature (along with many others brought to the new stand, I'm sure) were introduced in the Boost set of libraries first.

      • constexpr (a far more expressive metaprogramming utility than templates)

        That is incorrect.

    • Don't forget initializer lists, variadic templates, non-static data member initializers, finally fixing that Template> (note the >>) thing, rvalues, nullptr, strongly-typed enums, constructor improvements (holy god we don't have to rewrite every fucking thing every fucking time or split off into an ::init()), user-defined literals which is crazy cool combined with templates and initializer lists, and lots of stuff I'm sure I'm forgetting about.

      Since starting on C#, I've kind of felt like I'm back in the dark ages in C++, even as it remains my favorite language. I've already started using a lot of these improvements, and while C++ still has it's rough edges, the improvement in "fun" while coding is massive. No more for (some_container_type<vector<map<int, string> > >::reverse_iterator aargh = instance.begin(); aargh != instance.end(); ++aargh) for me!

      • by Godai ( 104143 ) *

        Heh, thanks for filling in the rest. I knew there were more, but I was just throwing down what I could remember off the top of my head. I was watching Bjarne talk about C++11 at Going Native and now I'm anxiously awaiting our (long overdue) transition to VS2010 (from 2005). I know it doesn't cover all the good stuff, but it has a lot of the goodness in there. Though, I'm writing cross platform code with a Mac team that refuses to use Clang, so God knows when I'll be allowed to use anything from C++11 :(

        • Sometimes I get the feeling that my company is the only one left using VC++ 6. Our attempts at migrating get 10% completed and then abandoned when the next ultra critical super important demo/contract shows up.
          • firstly, I wish I was using VC6 still - the IDE was the best there ever was, fast, simple, didn't flicker or use up all your RAM.

            However, you can use a different IDE [wordpress.com] if you must keep the old compiler for backwards compatibility.

            Alternatively, you can replace the compiler [intel.com] in the IDE.

  • Unfortunately I code in java these days.. chances that oracle will see the light? :-)

    • by godrik ( 1287354 ) on Friday February 24, 2012 @04:13PM (#39152705)

      chances that oracle will see the light? :-)

      Last time they saw the Sun, it did not end well...

    • chances that oracle will see the light?

      Roughly the same as the chance that Larry Ellison will fly his MiG 25 straight into the side of a mountain.

    • For anything but the highest performance computing, Java gives the same performance. In fact, for service code, Java gives better performance. I've benchmarked a prime sieve with the same algorithm in Java, and C++ (with and without the -g flag). It's a good test of integer arithmetic+basic language performance because the compiler cannot optimize away loops. Java is only 5% slower than stripped C++. Java is significantly faster than C++ with the -g flag. But Java actually retains all the symbol and f
  • Small interview, not anything new is added. Trolls will enjoy the old Java versus C++ thing again.

    Seriously, a big disappointment.
  • by busyqth ( 2566075 ) on Friday February 24, 2012 @03:46PM (#39152229)
    S-expressions, continuations, hygenic macros...
  • by account_deleted ( 4530225 ) on Friday February 24, 2012 @03:48PM (#39152255)
    Comment removed based on user account deletion
    • Not seeing (1) (Score:5, Insightful)

      by SuperKendall ( 25149 ) on Friday February 24, 2012 @04:07PM (#39152609)

      I don't think you'll see a lot of people flaming C++, there just aren't that many people that care one way or the other anymore.

      I think some of the new features look nice but mainstream use has been shifting away from C++ for a while and I'm not sure I see these new features drawing many back...

      • Re:Not seeing (1) (Score:5, Interesting)

        by gbjbaanb ( 229885 ) on Friday February 24, 2012 @06:16PM (#39154087)

        Mainstream use is shifting back to C++ since Microsoft decided that C++ was batter than C# (or at least Sinofsky and the Windows team decided that was the case). Herb Sutter has done a lot of presentations on the new paradigm at Microsoft - do a quick google.

        Of particular note is the reason they're giving for this: in the datacentre native code reduces the amount of energy required to run your apps, and that adds up significantly if you're using dynamic or even JIT language.

  • by Bookwyrm ( 3535 ) on Friday February 24, 2012 @03:55PM (#39152351)

    In some ways, a lot of what is being added to C++ makes me think of Scala, just less readable.

    While the additions and extensions certainly make things more interesting and potentially more powerful/easier for the *individual* programmer, I look forward to seeing what sort of interesting train wrecks happen when larger teams try to make use of these features. I certainly hope the debuggers are being updated to be useful when someone's written a template that uses a #define'd macro to specify a closure that is passed through an anonymous function, etc.

    This strikes me as the next generation's 'multi-threading' -- where almost every programmer claims they can handle multi-threaded programming, but very few actually do it well. Particularly in teams when interactions require coordination. Going to take a whole new round of winnowing the wheat from the chaff when it comes to finding developers who can actually use these features well without driving their coworkers insane.

    • Time to overhaul the academics. Nearly every engineering course here (I'm in India) has a couple of programming courses. A lot of students do coding some time or the other. Yet not even a sentence is uttered about threads or parallelism, even though practically every computer they code in has multiple cores. They should probably introduce a course on parallel processing as an elective for freshmen.

    • by khipu ( 2511498 )

      Both C++ and Scala are attempts to modernize limited, poorly designed languages (C, Java) by adding things like type parameters and functional features. There are a bunch more of those attempts and I think they have mostly been failures. It's roughly like: "Never try to teach a pig to sing. It wastes time and annoys the pig"

  • FTA

    I expect to see the first complete C++11 compiler sometime in 2012

    I expect to see the first complete C++11 compiler sometime after I see the apocalypse.

    Oh right... I guess that would be December 2012, wouldn't it? :)

    Seriously... while a lot of compilers implement some of the features, I really don't think there's a hope in hell of seeing any real progression to adopting the standard. With C, the standard developed around what many compilers were already doing... ditto with the original C++ spec.

    • The majority of it is implemented. http://gcc.gnu.org/projects/cxx0x.html [gnu.org]

    • Re:He's optimistic (Score:5, Informative)

      by grumbel ( 592662 ) <grumbel+slashdot@gmail.com> on Friday February 24, 2012 @05:07PM (#39153489) Homepage

      > But C++11 describes a standard that absolutely nobody has ever got anywhere close to, so I don't imagine that there's going to be a lot of drive to adopt it.

      All popular C++ compilers already implement large parts of C++11, so the chance of seeing widespread C++11 adaption in the not so distant future is pretty high. Also this wasn't really any different with C++98, which essentially no compiler supported on release and which then took a few years to gain widespread adoption.

      • by jgrahn ( 181062 )

        All popular C++ compilers already implement large parts of C++11, so the chance of seeing widespread C++11 adaption in the not so distant future is pretty high. Also this wasn't really any different with C++98, which essentially no compiler supported on release and which then took a few years to gain widespread adoption.

        C++98 was worse. There was extern templates, which was a major mistake (read the proposal to remove it from C++11 for some amusement). There was Microsoft, who lost interest in C++ around that time, and condemned a generation of Windows programmers to Visual Studio 6 and a weak standard library implementation. And yet gcc was usable around 1999 or so. I started using the std namespace in late November that year.

  • by Daniel Phillips ( 238627 ) on Friday February 24, 2012 @04:24PM (#39152881)

    I guarantee you will quickly come to hate the new "narrowing" errors, for example any time int converts to float inside initializer curlies, or double to float. As a language feature, this lies firmly in the category of wanking.

    That said, my code is all full of lambdas now, thankyou. On the other hand, lambda syntax is uglier than sin. While a lambda can do anything a lexically scoped nested function can, it is not pretty. Now obviously, there is no reason not to support proper lexically scoped nested functions, as GCC already does. Arguing that they are now unnecessary because of lambdas is, again, wanking. The practical fallout is that when you move a lambda-style helper function to global scope or to member status, you have a bunch of editing to do, to remove the ugly auto/lambda bits.

    Another big disappointment is, nothing was done to address the good features C99 has that C++ does not. Designated initializers and variable size array parameters, to name two. Wanking again.

    On the whole, there is too much wanking going on in the C++ stewardship process. Guys, you need to remember, C++ does not exist only as a means of compiling the STL.

  • I was amused by the comment "If that incr(0) were allowed either some temporary that nobody ever saw would be incremented or - far worse - the value of 0 would become 1. The latter sounds silly, but there was actually a bug like that in early Fortran compilers that set aside a memory location to hold the value 0. "

    Back then, I wrote Fortran subroutines which took computed dimension arrays by declaring the arrays with crazy bounds, numbers I hoped would never be used as constants, and then "assigning" the re

  • I watched a few of the "Gone Native" webcasts on the C++ extensions, and it's crazy what they're doing with the language. Are the features useful? Yes, but they're taking a complex language and slapping on yet more functionality. Some new C++ code syntax doesn't even *look* like C++ anymore it's so different. Not everyone is a C++ guru and the language is bad enough supporting so many different paths to the same implementation outcomes. This is just going to make staffing, testing, training, and code r

    • by euroq ( 1818100 )

      They need to just start from scratch and create a limited subset of features that doesn't pretend to be C and doesn't lug around all of the past mistakes in the standard, and call it C+++.

      They did. Check out the D Programming Language.

  • Probably the last thing preventing it from being truly safe and useful in shared libraries across implementations.

    How many years has it been?

  • and these new features?
  • Meh... (Score:3, Interesting)

    by jythie ( 914043 ) on Friday February 24, 2012 @07:05PM (#39154567)
    I gave up on C++ years ago. It has really become a 'geek cred' language with a constantly changing 'right' way an aesthetic, perfect for figuring out if a fellow geek is from the same snapshot of teaching you came from, but that is about it. It has become overly complex with redundant language features that one needs to keep relearning in order to understand other people's code.. and of course with complexity comes the ability to show off your knowledge through doing things in 'clever' ways.

    Good for showing off.. bad for getting actual work done, esp for projects that last more then a year or two.
    • by jgrahn ( 181062 )

      I gave up on C++ years ago. It has really become a 'geek cred' language with a constantly changing 'right' way an aesthetic, perfect for figuring out if a fellow geek is from the same snapshot of teaching you came from, but that is about it. It has become overly complex with redundant language features that one needs to keep relearning in order to understand other people's code.. and of course with complexity comes the ability to show off your knowledge through doing things in 'clever' ways. Good for showing off.. bad for getting actual work done, esp for projects that last more then a year or two.

      Well, what are the alternatives? I my domain, it's plain old C. Java is not an option, and from the little I hear fashion changes *more* quickly there anyway.

      I think your impression of the changing "right" way is warped, or perhaps you're in a subculture I'm not familiar with. I see these different ways:
      (a) C with classes, from C programmers who've read a book but don't quite get it.
      (b) OOP/Design Patterns rule! Popular in the mid-1990s. Lots of inheritance and stuff; every piece of code aspiring to gen

    • by Greyfox ( 87712 )
      I've been doing some system-level programming and... stuff... with the language again lately. I was getting tired of having to load a language VM to run my code (Flavors of perl, Ruby or Java) and not having a whole lot of control over the hardware I'm running on. I'm just as happy rolling up a raw process and seeing how little memory I can get away with using. A mostly-C socket server and a tiny little btree class is all I need to serve up some legitimately useful information with a memory footprint under

news: gotcha

Working...