Biggest Changes In C++11 (and Why You Should Care) 385
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."
13 years? (Score:2, Interesting)
Re: (Score:3)
A post under you already explained it: it refers to the first ISO standard for C++. And yeah, I thought the same thing: "Really? Only 13 years?"
Re:13 years? (Score:5, Funny)
What struck me was that C++ got lambda expressions before Java did!
Re: (Score:3)
C++ have had lambda expressions for more than a decade, they were just really inconvenient to declare and use. Now it is easier.
Re: (Score:3)
i've never heard of such a thing. Then again I'm not (primarily) a c++ guy. Care to provide an example of lambdas before C++ 2011?
Re:13 years? (Score:4, Informative)
The previous C++ standard, C++98, is 13 years old, as the name implies.
Re: (Score:3)
RAII is not important to me and does not implement anything I can't do myself, with extremely fine grained control. To "express" this functionality, I check my allocations, I check my return codes and states, I make sure I provide same, I only do new things based on the known state of things I've already done, and I set up state-testing, abort-capable monitors for operations that might conceivably go open-ended. I try really,
13 years? (Score:5, Informative)
The article is probably referring to the first finished C++ ISO standard, 14882:1998. Hardly the "first iteration" of the language.
Re:13 years? (Score:4, 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.
Re: (Score:3)
It was developed by Bjarne Stroustrup starting in 1979
the C++ programming language standard was ratified in 1998
So you're saying the "first iteration" took 19 years. You must use the word "iteration" differently at your shop than we do at mine.
Re: (Score:2)
Re: (Score:3)
No. He used templates and built it statically. It finished building on Unix in 81. The compiler didn't finish on windows until 98 because of all of the constant win api changes.
Re:13 years? (Score:5, 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.
Re:13 years? (Score:4, Funny)
Re: (Score:3)
Re:13 years? (Score:4, Funny)
Now that's waterfall development!
Re: (Score:2)
Nope, the zeroth iteration.
Nice but... (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 -_-
Re: (Score:2, Insightful)
If you are putting binary flags in enums then you are using them wrong. And that's why you are no longer allowed to do so. It's a GOOD thing.
You want bitfields: http://msdn.microsoft.com/en-us/library/ewwyfdbe(v=vs.71).aspx [microsoft.com]
Re:Nice but... (Score:4, 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.
Re: (Score:2)
Re:Nice but... (Score:4, Interesting)
What so wrong with "const int SOME_BINARY_FLAG = 0xff00ff"?
Re: (Score:2)
Agreed. Low level language features exist for a reason, but their use (outside of a few specialized fields where bit-level optimization is preferred to code-readability-and-use optimization) is limited. Making it slightly less convenient (ie: use an int like we always have) for a (relative) few, while encouraging the many to use the more comprehensible variant, is not a bad idea.
Re: (Score:3)
C++11 has fixed that with the inclusion of the constexpr keyword, which allows you to define compile time constants, functions, and expressions.
For example:
constexpr uint32_t some_binary_flag = 0x8000000u;
constexpr int add(int x, int y) {
return x + y;
}
Re: (Score:3)
Speaking as someone who works on a C++ compiler - you're entirely wrong, for several reasons. The const qualifier means that the compiler can assume that it doesn't change. It can, in the first constant propagation pass, replace all loads of the constant with its value, and can then remove the memory allocation if it's static qualified, because it has no users. In fact, if it's static qualified and not const then, at a higher optimisation level, most compilers will work out that it never has its address
Re: (Score:2)
It is the most used way, not the best one. It is an inappropriate way. Even if bitfields have drawbacks, simple constants defined in a namespace remain a better choice.
It depends on what you mean by the word "best." There must be some reason that doing it this way is the most used way. Evidently, for many, it seems that it is the "best" way to do it. Since "best" is such an ambiguous word, maybe it would be better to elaborate on why it is inferior? Does it use more memory? Is it slower to execute? Is it more difficult to maintain? Depending on one's needs, the answer to any of those questions might influence what "best" means.
Re: (Score:2)
"There must be some reason that doing it this way is the most used way. "
Probably laziness. The fact that a lot of people sue it does not make it good: see: Access.
Best as in: It's poor engineering to use them. If you don't understand that, please stick to VB.
Re: (Score:2)
Not if the bitfields are volatile, which is common for hardware registers.
Bitfield layout portability or lack thereof (Score:2)
I was under the impression that bitfield layout was implementation-defined, and that's undesirable if I want my project to compile on different architectures. The page you linked includes the phrase "Microsoft Specific" around anything mentioning layout, which illustrates my point.
I was also under the impression that code generated by compilers for reading and writing bitfields was still dog slow. Has this changed?
Re: (Score:2)
You are basically saying. "I don't want C++, I want C!"
Re: (Score:2)
Re: (Score:2, Interesting)
Would love to use these features in the new C++,...
Why? What is it about these new features that will make your job easier, your code more reliable, and easier to be maintained? Or do you just want to use those features because they're "new" - for C++ that is?
As a long time C++ guy (Borland C++ days), I look at some of these features and think "so what?" (Lambda functions, please.) I'll probably never use them. IMHO the last truly useful feature that C++ added was Templates which lead to the STL and made my life much easier - after I got the hang of the way
Re:Nice but... (Score:5, Insightful)
Re: (Score:3)
This is definitely true. Concepts were supposed to fix this problem, but they were booted out at the last minute. Like most compiler errors though, you start associating those walls of text with common errors after a few times. STL isn't the worst offender by far in my opinion though, boost is. Most of Boost is hard to really consider C++ anymore, its template meta programming. C++ is there in the core, but if you show a circa 1998 (IE pre-templates) developer most boost libraries, they probably wouldn't re
Re: (Score:3)
Strong typing without type inference is just a stupid idea that should never have been allowed in a production language.
Re: (Score:2)
You're right, that is very cool.
Re: (Score:2)
they decided to make the name Y2K compliant this time
Yes, because a lot of people might be confused, thinking the standard came out in 1911. ;-)
Regarding the type inference, I always found that to be a glaring deficiency in STL. I always thought Borland's style of implementation was much better to use, although I can understand it wasn't as flexible or fast as templates. I did the same thing in my own class library back before I used templates (and they were standard), so I could do for-loops like this:
Re: (Score:3)
If your collection type has .begin() and .end() you can do
for (auto& v : collection) { ... }
...and 'v' will be a reference to the value_type of the collection that refers to the element currently being iterated over. It's basically short for
for (auto i = collection.begin(); i != collection.end(); ++i) { auto& v = *i; ...
}
You can of course also use it without the &, but then you make a local copy of every element.
Re: (Score:2)
As a long time C++ guy (Borland C++ days), I look at some of these features and think "so what?" (Lambda functions, please.) [...] the STL and made my life much easier - after I got the hang of the way the STL implemented things such as "iterators" and the gotchas associated with them.
When you use iterators, then you probably use algorithms like transform or for_each. for these, lambdas are a perfect supplement.
Re:Nice but... but nothing. They are useful. (Score:3)
Would love to use these features in the new C++,...
Why? What is it about these new features that will make your job easier, your code more reliable, and easier to be maintained? Or do you just want to use those features because they're "new" - for C++ that is?
As a long time C++ guy (Borland C++ days), I look at some of these features and think "so what?" (Lambda functions, please.) I'll probably never use them. IMHO the last truly useful feature that C++ added was Templates which lead to the STL and made my life much easier... I stopped reading at this point... .
Let me stop you right there for a second. Proper utilization of lambdas and closures pretty much make a lot of design patterns (template, strategy, visitor, for example) unnecessary in many contexts. This obviously will have an impact in how you write/use templates. Furthermore, lambdas and closures help with shrinking class hierarchies even further than delegation alone.
Yes, we had functors before, but now you neither need to define functors classes/structs defining 'operator()', nor have to invoke new o
Re:Nice but... (Score:5, Informative)
Re: (Score:3)
You can do this in C++ pretty easily by overloading the bitwise OR operator on the enum. Thus, when you have an enum type that you know is safe to manipulate as a set of orthogonal (or mostly orthogonal) bitfield values (such as the macros that define flags for open(), for example), you can add the appropriate "operator|" to work on that type.
Example:
#include <iostream>
using namespace std;
enum foo
{
WILMA = 2,
BARNEY = 4,
BETTY = 8
};
foo operator | (foo
Cruft removed? (Score:5, 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.
See wikipedia (Score:3, Informative)
http://en.wikipedia.org/wiki/C%2B%2B0x#Features_to_be_removed_or_deprecated [wikipedia.org]
Re: (Score:2)
Thanks. I wish that list was longer, but it's better than nothing.
Re: (Score:2)
They can't because it would break people's code and most people get upset when that happens.
In a Google Talk about the Go language, Rob Pike make a snide remark about how they made the recent iteration of Go smaller unlike some other languages. The only reason they can get away with that is because there's very little Go code out there.
In contrast, there's tons of C++ and Java code out there. Both languages have cruft that I'd like to see removed too, but it
Time for a flag day (Score:2)
They can't because it would break people's code
Then perhaps C++ and Java are overdue for a flag day [catb.org]. Visual Basic had one from VB6 to VB.NET, and Python more recently had one from Python 2 to Python 3.
Re: (Score:2)
Adding new features makes the code non-backwards compatible. Removing them does the same thing.
I've seen it happen in other languages (ruby, for example) and it wasn't the end of the world. If you really, really need to keep compiling old code, you just use an older compiler.
Looks cool... (Score:2)
I am glad that C++ is still evolving. That last major improvement I remember was the addition of the string class. Then shortly after that my professional focus moved away from C and C++ and towards higher level languages (.NET, PHP, Python, Java, etc...). I just recently started my own personal project so I decided to relearn C++ again, and I noticed there is a fair amount of new stuff that wasn't there before (or I was never taught)
Re: (Score:2)
I am really impatient to try this new language, but previous experience suggests I will have to wait at least 3 years before seeing a standard compliant compiler.
Re: (Score:3)
GCC has it mostly implemented (the new concurrency features remain MIA along with a few other bits) already, including the variadic templates the GP mentions.
http://gcc.gnu.org/projects/cxx0x.html [gnu.org]
Oh Snap. (Score:2)
So... (Score:2)
Looks like C# from a few years ago. Honestly, it's really good that they're moving C++ forward, it's been lacking these features when other languages have embraced them for some time. I see they still use a plethora of ugly ass underbars, though.
Re: (Score:2)
There's not reason for all languages to look the same.
Re: (Score:2)
It makes me feel like I'm still programming in the 1980s. It's old and ugly. It's not a required "look", it's just an ancient custom. The compiler works just fine without underbars in names.
Re: (Score:2)
..Different languages are different for a reason, they do different jobs ...
C# is trying to be the universal very high level language that embraces all paradigms and can be used for everything ...
C is still a low level language allowing to program low level
C++ is stuck in the middle and so should cover the ground where you want to abstract away from the machine, but don't want a VM getting in the way, if it moves too far towards C# is will start to be ignored ...
Both C# and C++ seem to be falling into the t
Re: (Score:2)
I don't think there is any danger of that. You can make a C++ program just as low level as one in C, if you want. You can still compile any C code with a C++ compiler in C++ mode with generally only very minor tweaks to the expressions, and none of the tweaks changing the binary code generated. You can use as much of the added C++ features as you want; anywhere from none of them to all of them. In an ideal world there would be no reason to use a straight C compiler any more. The ways real life departs
Re: (Score:2)
Except when the C code uses C++ keywords. Words like new, this and delete are common enough that there are plenty of C programs using them.
Another good reason to use a C compiler to compile C code is that the compiler is much simpler, and most likely has fewer bugs.
Re: (Score:2)
C# is trying to be the universal very high level language that embraces all paradigms and can be used for everything
...except non-PC, non-Microsoft platforms. (For example, MonoTouch and Mono for Android are still priced prohibitively for hobbyists.)
But... (Score:5, Insightful)
This is news for nerds. Stuff that matters. I thought /. abandoned this stuff ages ago...
Re: (Score:2)
No, not at all. There are just as many articles like this as there was 12 years ago... there's just a lot more of political crap, so the ration had changed.
Biggest Change? (Score:5, Funny)
Alternative syntax (Score:3)
Re: (Score:3)
Re: (Score:3)
Re:Alternative syntax (Score:4, 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.
Re: (Score:2)
Even worse, it's _Bool with one underscore and uppercase B. If you #include<stdbool.h> you get a #define bool _Bool which you can undefine if you really need the bool name for your own code.
See: stdbool.h [opengroup.org]
Re: (Score:2)
Congrats you just broke every C++ program that used "lambda" (or whatever keyword you chose) as a variable/class/whatever name.
And yes, while it may seem minor to you and something that can be fixed with search-n-replace, that's something to be avoided at almost any cost.
Re: (Score:2)
Anyone who used 'lambda' as a variable class of method should be drummed out of the business. Seriously.
Re: (Score:2)
That's "std::string" to you, bud. Library names get put into a namespace like "std", but you can't do that with keywords.
If your program uses "string" as an identifier, then don't have "using namespace std;" or "using std::string;" in your program, and refer to the C++ library string as "std::string" everywhere you use it. It's a little awkward, but you don't have to figure out which identifier is which. Using "lambda" or "function" as a keyword means these are interpreted as keywords by the parser, a
Design by Committee (Score:3)
Re: (Score:2)
Huge indeed, but judging by the results, not particularly good.
A doc that needs to be read by more people is the C++ Frequently Questioned Answers [yosefk.com].
Re: (Score:2)
A doc that needs to be ignored is the FQA. The author seems to have some sort of thing about denigrating C++, and either doesn't know what he's talking about or lies a lot.
I went through one random section carefully. (If necessary, I can go through another one carefully and report.) It's bad.
Does TFA actually explain things? (Score:4, 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":
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:
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:
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:
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:
Instead of:
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:
Re: (Score:3)
The article messed up in a number of places as you surmise. You can use auto in for-loops. I don't know why the example didn't show that properly (I was scratching my head).
As for the swap example, what ends up happening is that with move semantics, you can go back to using the naive version, but it will actually be efficient because under the hood, it uses the rvalue references instead of copying. It will behave is if it were written using all that "pseudo-code" (don't know why he called actual code "ps
Re: (Score:3)
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.
The string implementation is just an example. The current problem is that it is difficult to implement move semantics without having a special case for every class. The introduction of rvalue references enables a class to implement both copy and move semantics in a way that lets other classes transparently move the objects around. The move semantics are that the content is copied but the original need not be preserved (since often the original is a temporary). It enables other code (template libraries for c
Re: (Score:3)
Second, here's the newer, better syntax:
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++.
Yes, that is what the code looks like without move constructors. That has nothing to do with C++11, and yes, it's really ugly.
This that follows is the declaration (not implementation) of a move constructor.
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:
Now you can do
Movable foo = bar();
and it'll call the move constructor rather than the copy constructor when constructing foo. The advantage is important. For example, in the case of a string or a vector you don't copy the contents with the copy constructor just to delete the original when the rvalue goes
Re: (Score:3)
std::vector<int> vi;
for(int& i : vi)
Shouldn't they call this... (Score:2)
C+=10
?
Ignore this article (Score:2)
Do not read this article, it makes C++0x look bad by giving terrible examples of the new features. Even features I've been excited about look stupid after reading this. The article shows how *not* to use a lambda expression. A regular for loop would be better here. Using "auto" on int and long does work, but defeats the purpose. The second example of auto doesn't even make sense since it doesn't actually include the word auto. It should be something more like: auto ci=vi.begin();
Just ignore this and g
Why is a garbage collector even needed? (Score:4, 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?
So, why should I care... (Score:2)
...if I'm not a C++ programmer today?
I stated as a C programmer in the 1980s. I've used three different object-oriented extensions to C, and C++ was neither the first nor the best of them. I'm not in an industry (like video gaming) that pushes me toward C++ with any pressure at all. Every few years I take a look at C++ and conclude that it's safe for me to continue ignoring it.
Is there anything different this time around that would change this, that's easy to explain to someone who's not already a C++ pr
I stopped caring about C++... (Score:3)
Re: (Score:3)
Yeah but this one goes to eleven!
Re:Still playing catch-up to C#. (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...
Re: (Score:2)
Pretty much everything new in C++ 0x was around in the Boost libraries before showing up in C#. (A lot of the Boost stuff is written by C++ committee members). Standards usually follow years behind use. (The SCSI standard is my favorite for that- you know a particular SCSI rev is obsolete when the standard finally gets ratified).
These day you don't even need Boost: most of the stuff in the standard is already available in most compilers, if you set the right switches. Heck, even Visual Studio has a lot
Re:Still playing catch-up to C#. (Score:5, 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.
Re: (Score:2)
What's the patent FUD, specifically? I'm not talking about some obscure part of the Winforms API, I mean in the core language itself.
Any core language is almost useless without its standard and/or de facto libraries. Microsoft's patent policies on the libraries associated with C# (not just Winforms) are intentionally vague and confusing. Nobody really knows what conditions the patents may be used under, and by whom. This passive-aggressive stance on patents is a standard Microsoft strategy, and it's been pretty effective in general.
And you forget that C++ has a giant environment to install as well, but due to age, that is generally part of the OS as is.
Doesn't matter to the average user. If they have to worry about downloading and maintaining a huge pile of
Re: (Score:2)
You're worried about hassle for the "average user" who runs Sudoku apps. These users are on Windows. For tthose who are on Mac, Macs have a good enough package installation system that it's not too big of a deal for packages to have prerequisites.
BBut anyway,why are you citing external factors to somehow prove that C# iis an inferior language? Its "goodness" is in no way diminished by the fact that few platforms have implementations written for them.
Re: (Score:3)
Re: (Score:3)
Cause I really enjoyed the latest .net Framework 3.5 and 4 security updates that was nearly 400 MB... Thank you MS!
I also enjoyed the way it spent over an hour pre-compiling assemblies during the update. Granted, it was on an old machine, but it still would have been ridiculously long on a modern one.
Re: (Score:3, Insightful)
C++0x is C++11. C++0x was a placeholder name until they actually knew what year it would be finalised.
Re: (Score:3)
You know, like how Clash at Demonhead and Mega Man 2 took place in "200X!"
And it looks like they missed their worst-case deadline by 2 years!
Re: (Score:2)
Re: (Score:2)
And it shows they should never again make assumptions on finish dates. They never did get it done during the decade they targeted or assumed.
Re: (Score:2)
In hindsight (I posted the same thing), it occurs to me they can still claim to hit the target range if they go in hex...
But then it should have been C++ 0x0x
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
Does it have a 64-bit compiler yet?