Questions tagged [c++11]

Use this tag for code that must compile as C++11 (not using any features introduced in C++14 or later).

C++11 is the name of a version of the C++ standard approved in the year 2011. It replaced the previous standard, C++03, adding various core language changes and fixes, and an improved and expanded standard library. C++11 had earlier been referred to as C++0x, because it was originally expected to be published before 2010.

The ISO standard, 14882:2011, is withdrawn from the ISO website. The final draft was approved by the C++ working group on the 25th of March 2011. The publicly-available draft closest to C++11 is N3337, which has only editorial differences from the full standard.

Later versions of the language standard were approved and published in 2014 and 2017 (C++14 and C++17 respectively), and questions related to them bear the tags and here on StackOverflow. Each of these supersedes the previous, just as C++11 superseded C++03.

Please tag questions about C++11 with the tag, along with the tag.

Resources

New and Changed Features

53784 questions
355
votes
14 answers

When should you use constexpr capability in C++11?

It seems to me that having a "function that always returns 5" is breaking or diluting the meaning of "calling a function". There must be a reason, or a need for this capability or it wouldn't be in C++11. Why is it there? // preprocessor. #define…
Warren P
  • 58,696
  • 38
  • 168
  • 301
352
votes
4 answers

const vs constexpr on variables

Is there a difference between the following definitions? const double PI = 3.141592653589793; constexpr double PI = 3.141592653589793; If not, which style is preferred in C++11?
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
351
votes
10 answers

C++11 reverse range-based for-loop

Is there a container adapter that would reverse the direction of iterators so I can iterate over a container in reverse with range-based for-loop? With explicit iterators I would convert this: for (auto i = c.begin(); i != c.end(); ++i) { ... into…
Alex B
  • 75,980
  • 39
  • 193
  • 271
336
votes
8 answers

Rule-of-Three becomes Rule-of-Five with C++11?

So, after watching this wonderful lecture on rvalue references, I thought that every class would benefit of such a "move constructor", template MyClass(T&& other) edit and of course a "move assignment operator", template MyClass&…
Xeo
  • 123,374
  • 44
  • 277
  • 381
327
votes
5 answers

Start thread with member function

I am trying to construct a std::thread with a member function that takes no arguments and returns void. I can't figure out any syntax that works - the compiler complains no matter what. What is the correct way to implement spawn() so that it returns…
abergmeier
  • 11,287
  • 10
  • 49
  • 88
306
votes
10 answers

Should we pass a shared_ptr by reference or by value?

When a function takes a shared_ptr (from boost or C++11 STL), are you passing it: by const reference: void foo(const shared_ptr& p) or by value: void foo(shared_ptr p) ? I would prefer the first method because I suspect it would be faster.…
Danvil
  • 20,344
  • 18
  • 61
  • 85
304
votes
8 answers

Difference in make_shared and normal shared_ptr in C++

std::shared_ptr p1 = std::make_shared("foo"); std::shared_ptr p2(new Object("foo")); Many google and stackoverflow posts are there on this, but I am not able to understand why make_shared is more efficient than directly…
Anup Buchke
  • 4,300
  • 5
  • 19
  • 34
301
votes
7 answers

Can C++ code be valid in both C++03 and C++11 but do different things?

Is it possible for C++ code to conform to both the C++03 standard and the C++11 standard, but do different things depending on under which standard it is being compiled?
Erik Sjölund
  • 9,109
  • 7
  • 34
  • 60
292
votes
14 answers

When is std::weak_ptr useful?

I started studying smart pointers of C++11 and I don't see any useful use of std::weak_ptr. Can someone tell me when std::weak_ptr is useful/necessary?
user1434698
290
votes
1 answer

A positive lambda: '+[]{}' - What sorcery is this?

In Stack Overflow question Redefining lambdas not allowed in C++11, why?, a small program was given that does not compile: int main() { auto test = []{}; test = []{}; } The question was answered and all seemed fine. Then came Johannes…
Daniel Frey
  • 52,317
  • 11
  • 110
  • 168
285
votes
9 answers

How to make my custom type to work with "range-based for loops"?

Like many people these days I have been trying the different features that C++11 brings. One of my favorites is the "range-based for loops". I understand that: for(Type& v : a) { ... } Is equivalent to: for(auto iv = begin(a); iv != end(a);…
ereOn
  • 48,328
  • 33
  • 147
  • 228
281
votes
16 answers

Why doesn't C++ have a garbage collector?

I'm not asking this question because of the merits of garbage collection first of all. My main reason for asking this is that I do know that Bjarne Stroustrup has said that C++ will have a garbage collector at some point in time. With that said,…
Jason Baker
  • 171,942
  • 122
  • 354
  • 501
280
votes
1 answer

How is "int main(){(([](){})());}" valid C++?

I recently came across the following esoteric piece of code. int main(){(([](){})());} Reformat it as follows to make it more readable: int main(){ (([](){})()); // Um... what?!?! } But I can't get my head around how (([](){})()) is valid…
Mysticial
  • 438,104
  • 44
  • 323
  • 322
276
votes
9 answers

Meaning of = delete after function declaration

class my_class { ... my_class(my_class const &) = delete; ... }; What does = delete mean in that context? Are there any other "modifiers" (other than = 0 and = delete)?
Pat O'Keefe
  • 2,763
  • 2
  • 13
  • 5
275
votes
10 answers

Why does C++11's lambda require "mutable" keyword for capture-by-value, by default?

Short example: #include int main() { int n; [&](){n = 10;}(); // OK [=]() mutable {n = 20;}(); // OK // [=](){n = 10;}(); // Error: a by-value capture cannot be modified in a non-mutable lambda …
kizzx2
  • 17,547
  • 14
  • 72
  • 81