ISO/IEC 14882:2011
Sven Johannsen 14.03.2013 RWTH Institut für Geometrie und Praktische Mathematik |
sven@sven-johannsen.de |
C++11 - an Overview (part 1)
The ISO standard ISO/IEC 14882:2011 Programming Languages -- C++
define the C++ language together with the C++ standard library.
The availability of some more or less standard conform C++ compilers make it possible to compile the same source code with different compilers or choose between different compilers.
This make language like C++ (and C) different from languages like:
Developed by Bjarne Stroustrup at Bell Labs,
The new standard was expected in the late 2000: C++0x
But the changes are bigger as expected. After canceling of bigger features ("Concepts") the standard released in 2011: C++11
Feature | ||
---|---|---|
alignas (N2341) | alignof (N2341) | Atomic operations (N2427) |
auto (N1984, N2546) | C99 preprocessor (N1653) | Range-based for-loop (N2930) |
constexpr (N2235) | Defaulted And Deleted Functions (N2346) | decltype (N2343, N3276) |
Delegating Constructors (N1986) | Explicit conversion operators (N2437) | Extended friend Declarations (N1791) |
extern template (N1987) | Forward declarations for enums (N2764) | Inheriting Constructors (N2540) |
Initializer Lists (N2672) | Lambda expressions (N2550, N2658, N2927) | Local and Unnamed Types as Template Arguments (N2657) |
long long (N1811) | Local and Unnamed Types as Template Arguments (N2657) | Namespace Association (N2535) |
New character types (N2249) | New function declaration syntax for deduced return types (N2541) | nullptr (N2431) |
Unicode String Literals (N2442) | Raw String Literals (N2442) | User-defined Literals (N2765) |
Right Angle Brackets (N1757) | R-Value References ( N2118, N2844, N2844, N3053) | Strongly-typed enums (N2347) |
static_assert (N1720) | Template aliases (N2258) | Thread-Local Storage (N2659) |
Unrestricted Unions (N2544) | Variadic Templates ( N2242, N2555) | Built-in Type Traits (N1836) |
override and final (N2928, N3206, N3272) | Attributes (N2761) | Non-static data member initializers (N2756) |
Memory model | Libraries |
Feature | ||
---|---|---|
alignas (N2341) | alignof (N2341) | Atomic operations (N2427) |
auto (N1984, N2546) | C99 preprocessor (N1653) | Range-based for-loop (N2930) |
constexpr (N2235) | Defaulted And Deleted Functions (N2346) | decltype (N2343, N3276) |
Delegating Constructors (N1986) | Explicit conversion operators (N2437) | Extended friend Declarations (N1791) |
extern template (N1987) | Forward declarations for enums (N2764) | Inheriting Constructors (N2540) |
Initializer Lists (N2672) | Lambda expressions (N2550, N2658, N2927) | Local and Unnamed Types as Template Arguments (N2657) |
long long (N1811) | Local and Unnamed Types as Template Arguments (N2657) | Namespace Association (N2535) |
New character types (N2249) | New function declaration syntax for deduced return types (N2541) | nullptr (N2431) |
Unicode String Literals (N2442) | Raw String Literals (N2442) | User-defined Literals (N2765) |
Right Angle Brackets (N1757) | R-Value References ( N2118, N2844, N2844, N3053) | Strongly-typed enums (N2347) |
static_assert (N1720) | Template aliases (N2258) | Thread-Local Storage (N2659) |
Unrestricted Unions (N2544) | Variadic Templates ( N2242, N2555) | Built-in Type Traits (N1836) |
override and final (N2928, N3206, N3272) | Attributes (N2761) | Non-static data member initializers (N2756) |
Memory model | Libraries |
N1757 (VC2005, GCC 4.3)
The character combination >>
is always as right shift operator for C++98.
This results in errors with nested templates:
C++98: Error, C++11: OK
C++11 will recognize >>
as two single >
in the context of templates.
N1811 (VC2005) N1653 (GCC 4.3)
Some small extensions for a better C99 compatibility
New data type long long
.
long long int : at least as much storage as long int. [C++11]
<cstdint>
define sizes for integer types (same as 7.18 in the C standard): int8_t
, ..., uint64_t
, ...
Some preprocessor extentions to provider better C99 compatibility.
N2431 (VC2010, GCC 4.6)
New keyword nullptr
: Address 0
(NULL
) for any pointer type.
nullptr
is the only value of the type std::nullptr_t
.
The preprocessor replace the macro NULL
with the integer value 0
.
So the compiler "see" never NULL
and have to deal with integer as pointers.
The usage of NULL
increases the readability of source code, while the usage of nullptr
allows the compiler to find erroneous code.
N1720 (VS2010, GCC 4.3)
New keyword: static_assert
User defined check at compile time with user defined error text.
Example:
static_assert(sizeof(int) == sizeof(void*),"pointers don't fits into int!");
Generates the following error (msvc, 64bit):
main.cpp(3): error C2338: pointers doesn't fits into int!
No runtime costs!
N2532, N2672 Uniform initialization, Initializer Lists (VC2012 CTP Nov12, GCC 4.4)
C++11 provides a uniform initialization syntax and initializer lists.
C++98 |
---|
N2532, N2672 Uniform initialization, Initializer Lists (VC2012 CTP Nov12, GCC 4.4)
C++11 provides a uniform initialization syntax and initializer lists.
C++98 | C++11 |
---|---|
N2532, N2672 Uniform initialization, Initializer Lists (VC2012 CTP Nov12, GCC 4.4)
One syntax for
list initialization invoke a initializer_list constructor or a fitting constructor.
The initializer list constructor of std::vector
will be preferred before the constructor call.
N2532, N2672 Uniform initialization, Initializer Lists (VC2012 CTP Nov12, GCC 4.4)
No narrowing. (No conversion from a bigger type to a smaller type allowed.)
N1984 (VS2010, GCC 4.4)
Deducing the type of variable from its initializer expression
Example:
auto i = 3; // int auto p = new MyObject(); // MyObject*
New meaning for the keyword auto
.
The usage as storage class is not valid anymore.
The type will resolved at compile time!
No dynamic type - no costs at runtime!
N2343 (1.0),
N3276 (1.1)
VS2010 (1.0), VS2012 (1.1) GCC 4.3 (1.0)
Deducing the type of variable from an expression.
Can be used one the same places there a type (e.g. int) is allowed.
decltype(3+4)
is the type.foo()
or v.begin()
will not called!No costs at runtime!
N2541 (VS2010, GCC 4.4)
New Syntax to define a return type of a function.
auto f() -> int { return 3; }
Scott Meyers:
Non-lambda non-decltype uses not expected to be common.
N2541 (VS2010, GCC 4.4)
The new Syntax defines no different functions: The signature of a function is the same!
// myclass.h auto f() -> int; // myclass.cpp int f() { return 3; }
Deduction of the return type based of function parameters.
N2930 (VC2012, GCC 4.6)
Range-based for loop supports:
int[3]
).begin()
and .end()
.begin()
and end()
(begin/end must yield a “suitable iterator”.)
N2930 (VC2012, GCC 4.6)
N2928 v0.8, N3206 v0.9, N3272 v1.0 (GCC 4.7, v0.8: VS2005 , v1.0: VS2012)
override
: This function overrides a function of the base class.final
: It's not possible to override the function / the class.If someone changes the signature of Base::f
then the compiler will report an error
It's not possible to derive from EndKnot
.
N2346 (GCC 4.4)
Better control of automatic generated member functions.
To prevent an implicit copy of an object in C++98, declare a private copy constructor and operator=.
C++98
C++11
N2346 (GCC 4.4)
delete
and default
allows a better control of the generated member function and implicit conversions.
N2756 (GCC 4.7)
Allow non-static data members to be initialized where declared.
N1986 (VC2012 CTP nov'12, GCC 4.7)
Call a constructor from another constructor of the same class.
N1986 (VC2012 CTP nov'12, GCC 4.7)
A class is constructed, after the first constructor is processed.
E.g.: a destructor will called only for fully constructed objects.
N2437 (GCC 4.5)
In C++98 the conversion operators enables implicit conversions.
N2437 (GCC 4.5)
Explicit conversion operators gives the programmers more control:
N2347 (VC2010, GCC4.4)
C++ enum are not "strongly scoped"
Solution: "strongly typed enum class"
N2347 (VC2010, GCC4.4)
The size of an enum is not defined.
New syntax to specify a base (integer) type:
N2442 (VC2012 CTP Nov12, GCC 4.5)
with minor changes
Removed math functions, Otherwise minor changes
Modified per new C++11 language features; otherwise minor changes
ISO/IEC 19768:2007
<memory>
)<array>
)std::tuples
(<tuple>
)std::unordered_map
std::unordered_multimap
std::unordered_set
std::unordered_multiset
<regex>
)<type_traits>
)Beyond TR1:
<system_error>
(wrapper around <cerrno>
)std::forward_list
<ratio>
) numerator and denominator for kilo, milli, exa, peta, ... (works also for time)<chrono>
) e.g. std::chrono::system_clock::now()unique_ptr
(very similar to boost::scoped_ptr
)<thread>
<mutex>
<condition_variable>
<future>
e.g. std::async
, std::future
New Algorithms for C++11
new Algorithms in C++11 | |
---|---|
all_of (is p true for all e in R?) | is_partitioned (is R partitioned per p?) |
any_of (is p true for any e in R?) | partition_point (find first e in R where p(e) is false) |
none_of (is p true for no e in R?) | is_sorted (is R sorted?) |
find_if_not (find first e in R where p is false) | is_sorted_until (find first out-of-order e in R) |
copy_if (copy all e in R where p is true) | is_heap (do elements in R form a heap?) |
copy_n (copy first n elements of R) | is_heap_until (find first out-of-heap-ordered in R) |
iota (assign all e in R increasing values starting with v) | move (like copy, but each e in R is moved) |
minmax (return pair(minVal, maxVal) for given inputs) | move_backward (like copy_backward , but each e in R is moved) |
minmax_element (return pair(min_element, max_element) for R) | R is a range, e is an element, p is a predicate: |
partition_copy (copy all e in R to 1 of 2 destinations per p(e)) |
R is a range, e is an element, p is a predicate.
<memory>
<algorithm>
<system_error>
<thread>
, <chrono>
<condition_variable>
, <mutex>
<future>
<array>
, <tuple>
, <forward_list>
<unordered_map>
, <unordered_set>
<regex>
, <ratio>
<codecvt>
<initializer_list>
<type_traits>
<cstdbool>
, <cstdint>
<cuchar>
, <cwchar>
, <cwctype>
export
(for templates) is removed, but still reservedauto
as storage classregister
as storage classauto_ptr
(use unique_ptr)std::unary_function
, std::binary_function
, ptr_fun
, mem_fun
,..)bind1st
, bind2nd
(use bind or lambda instead)Professional C++ (Wrox Professional Guides) from Marc Gregoire, Nicholas A. Solter, Scott J. Kleper
http://www.amazon.com/Professional-C-Wrox-Guides/dp/0470932449
C++ Primer Plus (6th Edition) von Stephen Prata
http://www.amazon.com/Primer-Plus-6th-Developers-Library/dp/0321776402
C++11: Der Leitfaden für Programmierer zum neuen Standard from Rainer Grimm
http://www.amazon.de/11-Leitfaden-Programmierer-Standard-Programmers/dp/3827330882
iX 01/2012 S.134 C++-Programmierung
[ISO/IEC 14882:2011] Programming languages -- C++
http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=50372 (or ANSI Webstore http://webstore.ansi.org as 2012-Version for 30$))
C++0x/C++11 Support in
cpprocks: C++11 compiler support shootout http://cpprocks.com/c11-compiler-support-shootout-visual-studio-gcc-clang-intel/
CodeProject: Explicating the new C++ standard C++0x, and its implementation in VC10 von Ajay Vijayvargiya. (Lambda Expression) http://www.codeproject.com/Articles/71540/Explicating-the-new-C-standard-C-0x-and-its-implem#LambdaExpressions
Next C++ User Group NRW meeting:
20.03.2013 (19:00-22:30) Chaosdorf Düsseldorf.
Frank Birbacher (Inform, Aachen) will talk about Allocators in the STL and the changes in C++11 (for allocators) with a example of a simple allocator and about a allocator for shared memory. (Presentation starts at 20:00.)
Chaos Computer Club Düsseldorf
Hüttenstr. 25
40215 Düsseldorf
http://meetingcpp.com/index.php/eventreader/events/c-user-group-nrw-meeting-50.html
/
#