# When submitting an answer, when is it ok to use C++11? [duplicate]

Possible Duplicate:
When did the “c++” tag start to imply C++11 by default?

I see a lot of answers around SO that use features of C++ that I'm not familiar with. Upon further investigation, I find that they are part of the new C++11 standard. However, from my understanding, most compilers don't support much, if any at all, of the C++11 features, and those that get "close enough" are still considered to be somewhat buggy experimental. In respect to both questions and answers, how should recommending the use of C++11 features and code be handled? Should the asker specify if they can and want to use C++11?

As an asker myself, I typically work in an environment that does not support the use of C++11 (a small dev team, where one dev can't just up and decide to move to a new (or updated) version of a compiler to use a new feature), so it would frustrate me and inefficiently use the time of the answerer if I received an answer to my question that solves it perfectly, yet uses a feature that I can't access.

Edit: I understand that SO is not for one person, but all that may come across it in the future, especially when it will no longer be referred to as C++11.

Edit: I would like to add a noteworthy, in my opinion, comment made by Almo: "From this site: gcc.gnu.org/projects/cxx0x.html "Important: GCC's support for C++11 is still experimental." That does not sound like a de facto standard to me. I don't think you'd see a big corp basing business-critcal stuff on features the compiler said was "experimental"."

-

## migrated from stackoverflow.comMay 25 '12 at 14:48

This question came from our site for professional and enthusiast programmers.

## marked as duplicate by Flexo, Mat, Yi Jiang, Cody Gray, animusonMay 27 '12 at 4:24

Define “most compilers”. Current versions of most actively developed compilers do actually support some C++11. –  Konrad Rudolph May 25 '12 at 15:27
@Konrad Most don't support all of the features. –  Drise May 25 '12 at 15:29
@Drise None supports all features. But the overlapping (and hence usable) subsets have become rather large. This restriction is also meaningless since until very (!) recently, no compiler supported all of C++03 – and to be honest, I’m not sure any compiler odes now (even disregarding template export). –  Konrad Rudolph May 25 '12 at 15:34
@Konrad You made a good point, and I edited the question accordingly. –  Drise May 25 '12 at 15:36

I always use C++11 first and foremost in my answers. That makes the answer better because it will resist the sands of time. Remember that the goal of providing an answer on StackOverflow is not to help one person at a time. It's to help everyone that will ever come across that answer. When someone clicks a link to that answer in some search engine results page in five years time, they will be happy I did that.

Doing so also helps spreads awareness of the new features, making it more likely that people will upgrade their compilers, or put pressure on their vendors to upgrade them.

I do take in consideration the fact that people will be stuck with... second-grade compilers for a while, especially those using MSVC, so when appropriate and feasible I also provide alternatives using the old language.

If an asker cannot make use of C++11 answers, they should mention so in the question. That makes it clear that the limited environment is a part of the problem. It is the asker's job to describe their problem correctly, not the answerer's job to guess it. But even in those cases, I do not eschew from at least mentioning any C++11 features that would make this or that part of the solution easier. It doesn't hurt.

But one should go beyond considering C++11 when answering new questions. There are lots of old questions that now have potentially outdated answers in them! When you see an old question that can be better answered using new features, post a C++11 answer on it. That's why we have the Necromancer badge.

-
+1 for "the goal of StackOverflow". –  Kerrek SB May 25 '12 at 15:04
Without specifying which C++ the answer is for, it will be an out-of-date answer once the next standard hits. Perhaps C++ as a tag is a problem, and we should be tagging every C++ question with the correct version. –  Almo May 25 '12 at 15:09
@Almo - no other language does that on SO (e.g. Java, C#) –  Flexo May 25 '12 at 15:10
This is true, and I wonder about it in terms of SO answers' longevity. Imagine when you've got answers floating around from 3 different versions of C# or Java. How to distinguish between them without version numbers on the tags? –  Almo May 25 '12 at 15:12
Or just doing a little extra research? StackOverflow is not a substitute for thinking. –  R. Martinho Fernandes May 25 '12 at 15:16
@Mankarse so if I answer a C++03 question now, and leave it tagged C++, how does someone two years from now know if it's a C++11 or C++03 answer? It's in 2012. That doesn't really prove which one it is, since it's in the overlap where the new standard is in the process of being adopted. It would be obvious if the year were 2010 on the answer. I think you guys are just being obstinate for the sake of being obstinate because it's how you do things now. –  Almo May 25 '12 at 15:20
@Almo so you're arguing that when discussing C++ we should ignore C++11, which is now formally ratified as an international standard, indefinitely until some arbitrary point in the future when you happen to feel it's ok to use it by default? –  Flexo May 25 '12 at 15:22
@Almo if you ask a question and get two answers that use different algorithms (but same language features), how do you pick between the two? Just apply the same reasoning. –  R. Martinho Fernandes May 25 '12 at 15:22
@Almo - that's not true at all if one algorithm takes polynomial time and the other n log n then they probably won't "both work" at all. The reader has to read and comprehend the answer. Cargo cult programming is bad however you look at it. –  Flexo May 25 '12 at 15:35
@Almo - not really. The point of Stack Overflow is to answer questions and build a lasting resource. That's not just about the OP and solving their problem with a copy and paste solution that's about the people who find it via Google 5 years later. If a question can't have lasting value then it's "too localizsed" and should be closed. Great answers provide depth and insight to many people, not just free tech support for the OP. –  Flexo May 25 '12 at 15:38
@NicolBolas for a great many people C++11 is the present second guessing if someone is stuck in the past and neglected to mention it is pointless. –  Flexo May 25 '12 at 15:42
@awoodland: And for a great many people, C++11 is not the present. Who are you to decide that the future has become the present? Especially when major compilers (VC++) don't support even a plurality of the new language features? –  Nicol Bolas May 25 '12 at 15:43
@Nicol: Everyone, whether stuck on C++03 or not, should give toolchain details. If they don't ask a good question, they should expect to get answers that work for others but not for them. –  Ben Voigt May 25 '12 at 16:05
@Nicol: Since when has any toolchain exactly matched a version of the published standard? Toolchain details means toolchain details, not "C++03 more or less, plus support for auto keyword type inference, and an old proposal of rvalue references". –  Ben Voigt May 25 '12 at 16:15
@BenVoigt: I'm not sure what you're arguing. Tagging a question as C++11 means, at the very least, "This question deals with material from the C++11 specification." The question itself can go into specific details of specific toolchains. But having a tag to say when questions do and do not deal with C++11 is a good thing. As I said, it is a first step, not the last. –  Nicol Bolas May 25 '12 at 16:24

You can use any features from the current (i.e. C++ standard), unless the asker of the question has mentioned a specific compiler version, in which case you should use only what that compiler supports.

If you "typically work in an environment that does not support the use of C++11", and you don't mention that environment in your question, you have no one to blame but yourself if you can't use the answers you get.

-
right,, why should we answer for non conforming compilers by default? That'd be silly. –  Mooing Duck May 25 '12 at 15:11
@MooingDuck: In the case of C++03, answering for non-conforming compilers became the default, because most compilers didn't conform (e.g., only one compiler ever implemented export). –  Jerry Coffin May 25 '12 at 15:26
@Jerry: The question still should mention what compiler, since that helps in other ways too. –  Ben Voigt May 25 '12 at 15:32
@BenVoigt For those new to the programming scene, I doubt many would know how to provide much information, nevertheless know what features their compiler supports, or even know that there are new features. –  Drise May 25 '12 at 15:46
@Drise right, let's keep them in ignorance. –  R. Martinho Fernandes May 25 '12 at 15:46
@R.MartinhoFernandes And when they come back saying that it doesn't work? –  Drise May 25 '12 at 15:48
@Drise what's your point? There are perfectly practical ways to deal with bad/very poor questions already –  sehe May 25 '12 at 15:51
@sehe R. Martinho suggested we keep new programmers in ignorance. If we provide a solution in C++11 and the asker doesn't have a compiler that supports that feature, they will come back saying it doesn't work, and it could turn into "It works for [the answerer], see here?: [example shown of working output]". –  Drise May 25 '12 at 15:55
@Drise: If they can't provide compiler version information, when requested in a comment, then they need to look for another line of work. And we will explain when additional information is needed. BTW, anyone know a good blog article, similar to sscce.org, for howto include toolchain information in a question? –  Ben Voigt May 25 '12 at 16:03
@BenVoigt I don't see why a person new to programming and/or new to a command line environment should "look for another line of work" simply because they are new and unknowing. Were we not all at one point in the same position? Isn't the point of all this to extend knowledge? –  Drise May 25 '12 at 16:08
@Drise: If they were told in a comment how to find the version (e.g. gcc --version), and were still unable or unwilling to provide the information, then they have no future as a programmer. –  Ben Voigt May 25 '12 at 16:10
@BenVoigt I wholly agree. –  Drise May 25 '12 at 16:13

C++ is defined by a standard, not by any particular implementation or non-implementation. This is one of the many great things about the language. If a question asks about "C++", with no further qualification, it is asking about the language, not any particular implementation of it.

C++11 is the current version of C++, so unless the question specifically gives a requirement to work on a particular compiler or with a particular version of the standard, answers should assume that the question is asking about C++11.

If C++03 (or earlier) answers are given, they run the risk of being out of date as soon as they are posted. In the long term, the vast majority of C++11 will be implemented by every major compiler vendor, and so C++11 answers will provide far more value to the site over time.

That said, there is nothing wrong with an answer that point out cases where behaviour is different between C++03 and C++11 (or cases where particular compilers have trouble, for that matter), as this can provide a more complete answer to a given question. Answers that are only correct for C++03 (or only correct for a particular compiler) should be clearly marked as such, to avoid the spreading of misinformation.

-

I think the standard is more or less a wild herring here. Answers should be based primarily on what's generally available, not what the standard says should be.

Obviously, if the question directly specifies a compiler, you pretty much need to try to stay within the limits of that compiler (though, of course, if somebody specifies, for example, VC++ 4.1 or gcc 1.37, most people can be forgiven for not remembering exactly what shortcomings those exact versions had).

Lacking that, I think most answers should be based on what (most) current compilers implement more than on what the standard says they should do. Yes, it's nice if you can give an answer that will still be good in 10 or 20 years -- but it's much more important that it be useful now. If something changes enough to render an answer less than useful in the future, it can be edited or a new answer added when it becomes necessary/useful. An answer that nobody can use is useless even if it's theoretically correct.

Of course, the rules change a bit when (for example) the [language-lawyer] tag gets involved or somebody's asking specifically about a (possible) compiler bug. In such a case, it may be perfectly reasonable to post code that won't work with any existing compiler (e.g., "here's code I think should work, but not only your compiler fails with it, but all the others seem to also").

-

Until C++11 is the de facto standard, I'd say answers should only use C++11 if the question specifically states that it's ok to use it, either in the text or via a tag.

-
Adding C++11 answers today helps to ensure that the question and its answers have lasting long term value –  Flexo May 25 '12 at 14:53
Almo: C++11 is the de-facto Standard in the latest versions of VC++, gcc and Clang, is it enough ? Your criteria is hard to evaluate :x –  Matthieu M. May 25 '12 at 14:58
From this site: gcc.gnu.org/projects/cxx0x.html "Important: GCC's support for C++11 is still experimental." That does not sound like a de facto standard to me. I don't think you'd see a big corp basing business-critcal stuff on features the compiler said was "experimental". –  Almo May 25 '12 at 15:06
@MatthieuM. since it came from a standards organisation through a defined formal process isn't it technically a de jure standard? –  Flexo May 25 '12 at 15:07
@awoodland: I think Almo meant for de facto to mean used by default. –  Matthieu M. May 25 '12 at 15:09
@awoodland: "Adding C++11 answers today helps to ensure that the question and its answers have lasting long term value" At the expense of having little value now. We can deal with the future when it comes; it's better to deal with the present as the present. After all, we can always add answers later. –  Nicol Bolas May 25 '12 at 15:21
@Almo - If you look at this page instead http://gcc.gnu.org/gcc-4.7/cxx0x_status.html almost all core language features seem to be implemented. Why not use them? –  Bo Persson May 25 '12 at 15:24
@NicolBolas - most C++11 features can be worked around with more verbose code. It should be fairly clear from a lambda or std::bind for example how you'd write that in C++98/03. Half the library changes are in boost anyway. Ruling it out now when the feature set is stable and defined seems needlessly short-sighted though. –  Flexo May 25 '12 at 15:29
@BoPersson: Because, shocking though it may be to believe, not everyone uses GCC. And even those who do don't necessarily use GCC 4.7+. –  Nicol Bolas May 25 '12 at 15:37
@awoodland: "Half the library changes are in boost anyway." No, some of the library changes are in Boost. Some of them aren't. boost::bind is not the same as std::bind, and pretending that it is is actively dangerous. I see this attitude as little more than the "C++ orthodoxy" trying to force their vision of the language upon others. You know, the people who say that you must always use std::vector, and if you're working in an environment where you're not allowed, then you're not a C++ programmer and should quit and other nonsense. –  Nicol Bolas May 25 '12 at 15:47
If I may, I don't see why Almo received downvotes for this. It seems to be a perfectly legitimate answer, as this is a discussion. And it seems to have spawned some insightful comments as well. –  Drise May 25 '12 at 16:05
@Drise: This is meta. Votes here don't mean, "this is confusing / doesn't add to the discussion". They are used to indicate community consensus. –  Ben Voigt May 25 '12 at 16:07
@MrLister: Reputation works differently on meta. –  Ben Voigt May 25 '12 at 16:13
@MrLister: I see this feature-request You answered it. It says status-completed. Is it not fixed? –  Ben Voigt May 25 '12 at 16:19
@BenVoigt: Not that differently. We still question/answer ban people if they get enough downvotes. Granted, I think this policy (at least for answers) is really stupid on Meta, but that's the way it is. It's clear that downvotes on meta aren't any different. There may be a bigger threshold, but they're not different. –  Nicol Bolas May 25 '12 at 16:22