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
574
votes
9 answers

When should I really use noexcept?

The noexcept keyword can be appropriately applied to many function signatures, but I am unsure as to when I should consider using it in practice. Based on what I have read so far, the last-minute addition of noexcept seems to address some important…
void-pointer
  • 12,317
  • 11
  • 40
  • 60
493
votes
6 answers

What are the main purposes of using std::forward and which problems it solves?

In perfect forwarding, std::forward is used to convert the named rvalue references t1 and t2 to unnamed rvalue references. What is the purpose of doing that? How would that affect the called function inner if we leave t1 & t2 as lvalues? template…
Steveng
  • 5,051
  • 4
  • 14
  • 7
487
votes
22 answers

Is it possible to print a variable's type in standard C++?

For example: int a = 12; cout << typeof(a) << endl; Expected output: int
Jorge Ferreira
  • 88,967
  • 24
  • 112
  • 131
481
votes
4 answers

Why is list initialization (using curly braces) better than the alternatives?

MyClass a1 {a}; // clearer and less error-prone than the other three MyClass a2 = {a}; MyClass a3 = a; MyClass a4(a); Why?
Oleksiy
  • 30,852
  • 19
  • 65
  • 114
474
votes
6 answers

C++11 rvalues and move semantics confusion (return statement)

I'm trying to understand rvalue references and move semantics of C++11. What is the difference between these examples, and which of them is going to do no vector copy? First example std::vector return_vector(void) { std::vector tmp…
Tarantula
  • 16,620
  • 11
  • 48
  • 70
431
votes
7 answers

How do I pass a unique_ptr argument to a constructor or a function?

I'm new to move semantics in C++11 and I don't know very well how to handle unique_ptr parameters in constructors or functions. Consider this class referencing itself: #include class Base { public: typedef unique_ptr UPtr; …
codablank1
  • 5,425
  • 4
  • 17
  • 26
406
votes
9 answers

What is std::promise?

I'm fairly familiar with C++11's std::thread, std::async and std::future components (e.g. see this answer), which are straight-forward. However, I cannot quite grasp what std::promise is, what it does and in which situations it is best used. The…
Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
405
votes
6 answers

Returning unique_ptr from functions

unique_ptr does not allow copy construction, instead it supports move semantics. Yet, I can return a unique_ptr from a function and assign the returned value to a variable. #include #include using namespace…
Praetorian
  • 100,267
  • 15
  • 224
  • 307
398
votes
1 answer

C++ template typedef

I have a class template class Matrix { // .... }; I want to make a typedef which creates a Vector (column vector) which is equivalent to a Matrix with sizes N and 1. Something like that: typedef Matrix Vector; Which…
Notinlist
  • 14,748
  • 9
  • 51
  • 92
397
votes
0 answers

Pretty-print C++ STL containers

I would like to have a single template that once and for all takes care of pretty-printing all STL containers via operator<<. In pseudo code, I'm looking for something like this: template
Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
393
votes
5 answers

Compiling C++11 with g++

I'm trying to update my C++ compiler to C++11. I have searched a bit and I have come to the conclusion that I have to use the flag -std=c++0x or -std=gnu++0x, but I don't know many things about flags. Can anyone help me? (I'm using Ubuntu…
Rontogiannis Aristofanis
  • 8,185
  • 8
  • 37
  • 58
389
votes
7 answers

std::unique_lock or std::lock_guard?

I have two use cases. A. I want to synchronise access to a queue for two threads. B. I want to synchronise access to a queue for two threads and use a condition variable because one of the threads will wait on content to be stored into the queue by…
chmike
  • 17,648
  • 19
  • 66
  • 93
377
votes
14 answers

How do I activate C++ 11 in CMake?

When I try to run a CMake generated makefile to compile my program, I get the error that range based for loops are not supported in C++ 98 mode. I tried adding add_definitions(-std=c++0x) to my CMakeLists.txt, but it did not help. I tried this…
Subhamoy S.
  • 5,973
  • 8
  • 32
  • 49
364
votes
5 answers

What are inline namespaces for?

C++11 allows inline namespaces, all members of which are also automatically in the enclosing namespace. I cannot think of any useful application of this -- can somebody please give a brief, succinct example of a situation where an inline namespace…
Walter
  • 40,885
  • 16
  • 97
  • 176
362
votes
5 answers

How to use range-based for() loop with std::map?

The common example for C++11 range-based for() loops is always something simple like this: std::vector numbers = { 1, 2, 3, 4, 5, 6, 7 }; for ( auto xyz : numbers ) { std::cout << xyz << std::endl; } In which case xyz is an int. But,…
Stéphane
  • 17,613
  • 22
  • 82
  • 117