ISO/IEC 14882:2011

C++ runderneuert

C++11 - an Overview (part 1)
Sven Johannsen
14.03.2013
RWTH Institut für Geometrie und Praktische Mathematik

sven@sven-johannsen.de
sjohannsen@slb.com
www.sven-johannsen.de
@svenjohannsen

Content

C++11 - an Overview (part 1)

ISO-Standard

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:

C++ History

Developed by Bjarne Stroustrup at Bell Labs,

C++0x vs. C++11

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

New Features

List of features from apache.org

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

New Features

List of features from apache.org

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

right angel bracket

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.

long long, C99 preprocessor

N1811 (VC2005) N1653 (GCC 4.3)

Some small extensions for a better C99 compatibility

nullptr

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.

static_assert

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!

Uniform initialization and initializer lists

N2532, N2672 Uniform initialization, Initializer Lists (VC2012 CTP Nov12, GCC 4.4)

C++11 provides a uniform initialization syntax and initializer lists.

C++98

Uniform initialization and initializer lists

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

Uniform initialization and initializer lists

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.

Uniform initialization and initializer lists

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.)

auto

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!

auto

N1984 (VS2010, GCC 4.4)

Example:

decltype

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.

Example:

No costs at runtime!

trailing return types (new function declarator syntax)

N2541 (VS2010, GCC 4.4)

New Syntax to define a return type of a function.

auto f() -> int
{
  return 3;
}

Everybody should be able to read the new syntax!

Scott Meyers:
Non-lambda non-decltype uses not expected to be common.

trailing return types (new function declarator syntax)

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;
}

trailing return types + decltype

Deduction of the return type based of function parameters.

range-based for loop

N2930 (VC2012, GCC 4.6)

For loop for container (or arrays):

range-based for loop

N2930 (VC2012, GCC 4.6)

Can be used together with auto, const and &:

range-based for loop

N2930 (VC2012, GCC 4.6)

Range-based for loop supports:

(begin/end must yield a “suitable iterator”.)

range-based for loop

N2930 (VC2012, GCC 4.6)

range-based for loop

N2930 (VC2012, GCC 4.6)

Works like:

or:

override and final

N2928 v0.8, N3206 v0.9, N3272 v1.0 (GCC 4.7, v0.8: VS2005 , v1.0: VS2012)

If someone changes the signature of Base::f then the compiler will report an error

It's not possible to derive from EndKnot.

Defaulted And Deleted Functions

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

Defaulted And Deleted Functions

N2346 (GCC 4.4)

delete and default allows a better control of the generated member function and implicit conversions.

Non-static data member initializers

N2756 (GCC 4.7)

Allow non-static data members to be initialized where declared.

Delegating Constructors

N1986 (VC2012 CTP nov'12, GCC 4.7)

Call a constructor from another constructor of the same class.

Delegating Constructors

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.

Explicit conversion operators

N2437 (GCC 4.5)

In C++98 the conversion operators enables implicit conversions.

Explicit conversion operators

N2437 (GCC 4.5)

Explicit conversion operators gives the programmers more control:

strongly-typed enums

N2347 (VC2010, GCC4.4)

C++ enum are not "strongly scoped"

Solution: "strongly typed enum class"

strongly-typed enums

N2347 (VC2010, GCC4.4)

The size of an enum is not defined.

New syntax to specify a base (integer) type:

Raw string literals

N2442 (VC2012 CTP Nov12, GCC 4.5)

Library changes

History

Boost subset

with minor changes

TR1 (Technical Report)

Removed math functions, Otherwise minor changes

C++98

Modified per new C++11 language features; otherwise minor changes

New Libraries (C++11)

TR1 (Library Technical Report)

ISO/IEC 19768:2007

C++11 Library Extensions

Beyond TR1:

C++11 Library Extensions

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.

C++11 Library Extensions Summery

Extended headers
new headers
C99 wrappers

Deprecated or removed features

Removed
Deprecated:

Books

Links

C++ User Group NRW

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

/

#