Slashdot Mirror


Biggest Changes In C++11 (and Why You Should Care)

Esther Schindler writes "It's been 13 years since the first iteration of the C++ language. Danny Kalev, a former member of the C++ standards committee, explains how the programming language has been improved and how it can help you write better code."

22 of 385 comments (clear)

  1. 13 years? by Meneth · · Score: 4, Informative
    C++ has been around for at least 28 years. From Wikipedia: "It was renamed C++ in 1983."

    The article is probably referring to the first finished C++ ISO standard, 14882:1998. Hardly the "first iteration" of the language.

  2. Nice but... by genjix · · Score: 3, Interesting

    Would love to use these features in the new C++, but unfortunately none of the major compilers support the new for-syntax, in class initialization, deleting members and explicit specification of base class methods.

    Also I totally don't understand why enum class no longer casts to ints... it totally makes using binary flags impossible unless I revert back to using the old style enums. But then I need to do the ugly namespace myenums { enum myenum { foo = 4, bar = 8 ... }; } hack which makes nesting inside classes impossible -_-

    1. Re:Nice but... by Arlet · · Score: 3, Insightful

      Bitfields are not as flexible when you want to change several different bits (belonging to different fields) in a word using a single write.

    2. Re:Nice but... by mmcuh · · Score: 5, Informative

      GCC, which is probably the most used C++ compiler, supports the new for-syntax since 4.6, deleted member functions since 4.4, and explicit virtual overrides in the 4.7 development series.

    3. Re:Nice but... by DrXym · · Score: 4, Insightful

      Templates are fine for little classes but they got so abused by STL that it was not uncommon to see trivial syntax errors turn into enormous cryptic compiler errors spanning multiple lines of nested templates and typedefs, half of whom you'd never heard of. After poring over this enormous error for minutes or longer you might eventually discover you missed a * off some declaration.

    4. Re:Nice but... by JohnnyBGod · · Score: 3, Interesting

      What so wrong with "const int SOME_BINARY_FLAG = 0xff00ff"?

  3. Cruft removed? by kikito · · Score: 4, Insightful

    I really like that they added new stuff to the language but ...

    Have they *removed* anything at all from it? That's the only way I could get interested in that language again.

  4. But... by DeathToBill · · Score: 5, Insightful

    This is news for nerds. Stuff that matters. I thought /. abandoned this stuff ages ago...

    --
    Slashdot - News for Nerds, Stuff that Matters, in ISO-8859-1 Has just realised that beta makes this signature redundant
  5. Re:13 years? by crow_t_robot · · Score: 3, Informative

    After years of development, the C++ programming language standard was ratified in 1998 as ISO/IEC 14882:1998

    C++ didn't exist as a standardized language till 13 years ago. It was in development before then.

  6. Biggest Change? by hal2814 · · Score: 4, Funny

    C++ goes all the way to 11. It's one louder than other languages.

  7. Re:13 years? by vidnet · · Score: 4, Funny

    What struck me was that C++ got lambda expressions before Java did!

  8. Re:C++0x by LighterShadeOfBlack · · Score: 3, Insightful

    C++0x is C++11. C++0x was a placeholder name until they actually knew what year it would be finalised.

    --
    Spelling mistakes, grammatical errors, and stupid comments are intentional.
  9. Re:13 years? by Lord+Lode · · Score: 3, Informative

    The previous C++ standard, C++98, is 13 years old, as the name implies.

  10. See wikipedia by Anonymous Coward · · Score: 3, Informative
  11. Re:Still playing catch-up to C#. by rennerik · · Score: 5, Interesting

    Your comment caught some flack, but I couldn't help but make a similar observation as I read the spec. It seems that they are adding a lot of stuff to C++ that exists in C# (lambda expressions, delegated constructors, automatic deduction, initialization syntax, a dedicated null keyword, etc).

    Of course, they added a bunch of stuff that's also NOT in C# (since it's not necessary in a high-level language like C#), but I am glad that they are revamping C++ to incorporate some higher-level functions. Now we just have to wait for compilers to start adopting the new spec...

  12. Re:Alternative syntax by TheRaven64 · · Score: 3, Informative

    Would it be that painful to add a lambda keyword?

    Yes, actually. Adding keywords to a language is problematic, because lots of existing code will use them as identifier names. If you add a lambda keyword then you break any existing code that contains a variable, function, or type called lambda. C99 had some ugly hacks to get around this for bool: the language adds a __bool type, and the stdbool.h type adds macros that define bool, true, and false in terms of __bool.

    --
    I am TheRaven on Soylent News
  13. Re:13 years? by rjstanford · · Score: 3, Funny

    Now that's waterfall development!

    --
    You're special forces then? That's great! I just love your olympics!
  14. Does TFA actually explain things? by SanityInAnarchy · · Score: 3, Insightful

    It's been awhile since I've had to do any C++, so maybe I'm just missing something, but it seems like either there's a lot of retarded functionality here, or there's a lot of TFA which introduces a feature, even motivates it, but doesn't actually explain what the new version looks like. For example, with "Rvalue References":

    void naiveswap(string &a, string & b)
    {
    string temp = a;
    a=b;
    b=temp;
    }
    This is expensive. Copying a string entails the allocation of raw memory and copying the characters from the source to the target...

    Ok, first, what? I thought standard library string implementations were supposed to be efficient, and include some sort of copy-on-write semantics, which would (I would hope) make the above a shuffle-pointer-around instruction instead of a copy-data-around instruction.

    Second, here's the newer, better syntax:

    void moveswapstr(string& empty, string & filled)
    {
    //pseudo code, but you get the idea
    size_t sz=empty.size();
    const char *p= empty.data();
    //move filled's resources to empty
    empty.setsize(filled.size());
    empty.setdata(filled.data());
    //filled becomes empty
    filled.setsize(sz);
    filled.setdata(p);
    }

    Regarding the first comment, no, I really don't, unless the point is that this is what the code for "moving" would look like if implemented in older versions of C++. But also:

    If you’re implementing a class that supports moving, you can declare a move constructor and a move assignment operator like this:
    class Movable
    {
    Movable (Movable&&); //move constructor
    Movable&& operator=(Movable&&); //move assignment operator
    };

    Ok, cool... But where is this used in the "moveswapstr" example? Does this make the "naiveswap" example automagically faster? Or is there some other syntax? It doesn't really say:

    The C++11 Standard Library uses move semantics extensively. Many algorithms and containers are now move-optimized.

    ...right... Still, unless I actually know what this means, it's useless.

    It looks like there's a lot of good stuff here, and the article is decently organized, but the actual writing leaves me balanced between "Did I miss something?" like the above, and enough confusion that I'm actually confident the author screwed up. For example:

    In C++03, you must specify the type of an object when you declare it. Yet in many cases, an object’s declaration includes an initializer. C++11 takes advantage of this, letting you declare objects without specifying their types:
    auto x=0; //x has type int because 0 is int
    auto c='a'; //char
    auto d=0.5; //double
    auto national_debt=14400000000000LL;//long long

    Great! Awesome! Of course, this arguably should've been there to begin with, and the 'auto' in front of these variables is still annoying, coming from dynamically-typed languages. But hey, maybe I can write this:

    for (auto i = list.begin(); i != list.end(); ++i) ...

    Instead of:

    for (std::list<shared_ptr<whatever> >::iterator i = list.begin(); i != list.end(); ++i) ...

    It's almost like C++ wanted to deliberately discourage abstraction by making it as obnoxious as possible to use constructs like the above. Anyway, that's what I expected the article to say, but instead, it says this:

    Instead, you can declare the iterator like this:
    void fucn(const vector<int> &vi)
    {
    vector<int>::const_iterator ci=vi.begin();
    }

    --
    Don't thank God, thank a doctor!
  15. Re:Still playing catch-up to C#. by Waffle+Iron · · Score: 4, Insightful

    The saddest part about this whole C++0x ordeal is that they're still just playing catch-up to C#.

    True. In particular, C++ is light years behind C# in patent FUD. And C++ hasn't even started work on requirements for a 100MB "managed environment" for users to install before running their apps. Nor have C++ developers chosen a monkey species after which to name its 2nd-class-citizen cross-platform implementation.

  16. Why is a garbage collector even needed? by ifrag · · Score: 3, Insightful

    What is the big fuss about getting a garbage collector anyway? Why does it even matter? Good C++ code shouldn't need a garbage collector. If memory was allocated within an object then the destructor should be taking care of it. And with shard_ptr (which people should start using) it's taken care of within there anyway. Is this wanted so everyone can start coding sloppy C++ and forget about the delete calls? I suppose for those using some 3rd party library that behaves poorly and is totally out of your control it could be nice to stop that from leaking all over. Still, it should have been done right in the first place.

    I suppose there might be some argument for preventing excessive memory fragmentation. Is there some other benefit to having one?

    --
    Fear is the mind killer.
  17. Re:13 years? by siride · · Score: 4, Insightful

    I have to lol at the "constant win api changes" statement. The Win API bends over backwards for backwards compatibility. In Unix, especially Linux, outside of POSIX (which is fairly limited in functionality), backwards compatibility is almost a 4 letter word.

  18. Re:13 years? by TheRaven64 · · Score: 3, Funny

    ModParentUpEx64!

    --
    I am TheRaven on Soylent News