MICC JINR Multifunctional
Information and Computing
Complex

RU

New opportunities of C++11

ROOT 6 is written in standard C++11. To successfully use this version of ROOT, you should get acquainted with differences of C+11 from previous versions of C++. Here is a list of the most significant ones:

Changes affecting the basis of the language:

1

New reserved keywords, namely, alignas, alignof, array, char16_t, char32_t, constexpr, decltype, index, noexcept, nullptr, static_assert, thread_local, are defined. Below each of these words is considered separately.

2

New data types: long long int,char16_t, char32_t are added.

3

The concept of rvalue links (defined with & amp; & amp;) to distinguish a link to lvalue (an object that has a name) and a link to rvalue (an object that does not have a name) is introduced. References to temporary values ​​(the rvalues ​​ values ​​on the right side of the assignment) allow you to change such values ​​after they are initialized. The class /structure used to have some implicit member functions: a default constructor (if another constructor is not defined), a copy constructor and a destructor. The copy constructor performs bitwise copying of variables. It means that if there is a class with pointers to some objects, the copy constructor copies the pointers, not the objects they point to. If it is necessary to get the objects in the copy, not just the pointers to them, one should describe it in the copy constructor. In C++11 a transfer (move) constructor and a move assignment operator are added. These two special functions take the T & & parameter, which is rvalue. In fact, they can modify the object.

4

Generic constant expressions based on the use of the keyword constexpr are added. With this qualifier, you can create variables, functions, and even objects, which will be calculated at the compile stage. Previously, it was necessary to use templates for such purposes. The constexpr keyword before the function indicates that the function always returns a constant value and allows you to use such a function in the same context as a constant.

5

The definition of the type of simple data (plain old data or POD) has changed. A simple data structure has a rigidly defined arrangement of fields in memory that does not require access restrictions and automatic control. Variables of this type can be copied with simple procedures of copying plots of memory. In C ++ 11, it is split into two: the trivial type (allowing static initialization and use of memcpy) and the type of standard placement (the order of its members is compatible with the order C). It allows one to more accurately set the boundaries of type compatibility with C.

6

The concept of an external template is introduced, i.e. a way to prevent the compiler from instantiating (create a class instance) a pattern.

7

Options for using initialization lists are expanded. The concept of initialization lists came in C ++ from C. A structure or an array can be created by passing the list of arguments in the order corresponding to the order of definition of members of the structure. Initialization lists are recursive, which allows using them for arrays of structures and structures containing nested structures.

struct Object

{

    float first;

    int second;

};

Object scalar = {0.43f, 10}; // one object from first=0.43f and second=10

Object anArray[] = {{13.4f, 3}, {43.28f, 29}, {5.934f, 17}}; // array of three objects

Initialization lists are very useful for static lists and in cases when you need to initialize the structure with a specific value. C++ also has constructors that can contain a general part of the work on the initialization of objects. Standard C++ allows initialization lists to be used for structures and classes, provided that they meet the definition of a simple data type (Plain Old Data POD). Classes that are not PODs cannot be used for initializing initialization lists, including standard containers of C++ such as vectors. C++11 combined the concept of initialization lists and a template class named std :: initializer_list. This allowed constructors and other functions to get initialization lists as parameters. For example:

class SequenceClass

{

public:

  SequenceClass(std::initializer_list list);

};

This description allows you to create SequenceClass from a sequence of integers as follows:

SequenceClass someVar = {1, 4, 5, 6};

The std :: initializer_list <> class is defined in the C++11 standard library. However, objects of this class can be created by the C++11 compiler only statically using syntax with brackets {}. The list can be copied after creation; however, it will be copying by reference. The initialization list is constant: neither its members nor their data can be changed after creation.

8

C++11 provides a syntax that allows you to use a single form of initialization for all types of objects using the initialization list syntax extension:

struct BasicStruct {

    int x;

    double y;

};

struct AltStruct {

    AltStruct(int x, double y) : x_(x), y_(y) {}

private:

    int x_;

    double y_;

};

BasicStruct var1{5, 3.2};

AltStruct var2{2, 4.3};

Universal initialization does not completely replace the initialization syntax using the constructor. If the class has a constructor that takes the initialization list as an argument (TypeName (initializer_list );), it will have a higher priority than other features to create objects. For example, in C++11 std :: vector contains a constructor that takes the initialization list as an argument:

std::vector theVec{4};

This code will call the constructor that takes as an argument the initialization list, rather than a constructor with a single parameter that creates a container of a given size. To call this constructor, you should use the standard syntax for calling a constructor.

9

Type inference capacities are added: variables initialized during declaration can be declared as auto, and the decltype keyword allows you to determine the type of a variable at the compilation stage. Prior to C ++ 11, the auto keyword was used as a variable storage specifier (such as register, static, extern). In C ++ 11, auto allows you not to specify the type of a variable explicitly, telling the compiler to determine the actual type of the variable itself, based on the type of value being initialized. It can be used when declaring variables in different scopes, such as namespaces, blocks, initialization in a loop, etc. auto i = 42; // i - int auto l = 42LL; // l - long long auto p = new foo(); // p - foo*

10

The foreach loop syntax for iterating over the collection elements is added. In the new form, it is possible to perform iterations if the begin () and end () methods are overloaded for the iteration object. It is useful when you just need to get items of the array / container or do something with them without worrying about indexes, iterators, or the quantity of elements.

11

Support for anonymous lambda functions is added.

12

A new syntax for declaring function templates, indicating the type of the return value after listing the function arguments is added.

13

"Smart pointers" with reference counting and automatic memory deallocation are introduced: unique_ptr: should be used when the memory resource should not be shared (it does not have a copy constructor), but it can be transferred to another unique_ptr shared_ptr: should be used when the memory resource is to be shared weak_ptr: contains a link to the object controlled by shared_ptr, but does not count links; allows you to get rid of cyclical dependence.

14

Class constructors can call other constructors of the same class as in Java, C #, and D. The object is considered to be created as soon as at least one constructor has worked.

15

Two new identifiers are added: override, to indicate that the method is an override of the virtual method in the base class and final, indicating that the derived class should not override the virtual method.

16

A new nullptr keyword that defines a null pointer is introduced. Previously, the null macro was used to reset pointers; it is a zero integer type, which, of course, caused problems (for example, when overloading functions). The nullptr keyword has its own type std :: nullptr_t, which eliminates these problems. There are implicit conversions of nullptr to a null pointer of any type and to bool (as false), but there is no conversion to integer types.

17

The “traditional” enumerations in C ++ have some drawbacks: they export their values ​​to the surrounding scope (which can lead to name conflicts); they are implicitly converted to the integer type and cannot have a user-defined type. These problems are fixed in C ++ 11 with the introduction of a new category of enumerations called strongly typed enums. They are defined by the enum class keyword. They no longer export their enumerated values ​​in the surrounding scope, are no longer implicitly converted to the integer type and can have a user-defined type (this option is also added for “traditional” enumerations).

enum class Options {None, One, All};

Options o = Options::All;

18

Changing the specification of the parser so that >> is interpreted as a pair of closing brackets, and not as a right shift operator.

19

The explicit keyword also applies to operators of type conversion (to avoid implicit transformations).

20

Synonyms for templates.

21

Some restrictions on what types of objects can belong to the union are removed.

22

Templates with a variable number of parameters are allowed.

23

Support for three new types of string constants: UTF-8 (u8 "..."), UTF-16 (u "...") and UTF-32 (U "..."), custom constants (the ability to interpret a record as a constant of some type depending on its suffix).

24

Multi-threaded memory model.

25

Storing data local to the thread.

26

Static_assert, which checks the assertion at the compilation stage, is added. If the statement is true, nothing happens. If false, the compiler displays the specified error message.

27

Sizeof can work with class members without objects of this class.

28

An ability to control the placement of variables is added.

29

Some scripts that made the implementation of automatic garbage collection impossible are removed.

30

The begin () and end () functions are introduced. This is a new addition to the standard library. They work with all STL containers and can be extended to work with any type.

 

STL library changes:​

1

Multithreading support (classes of threads, mutexes, condition variables, locks, etc.).

2

A tuple (std :: tuple), i.e. a non-uniform list of elements, is added. The tuple is "similar" to a couple (std :: pair), however, it can contain the arbitrary (albeit finite) number of elements. You can create a tuple like this:

#include

std::tuple tuple_("hello", 42, 3.14);

A tuple can be called a collection of heterogeneous (disparate) values. Therefore, the element type (value_type), which is defined in any standard container, does not make sense in the tuple, and is simply absent, just like any of the iterator types. The tuple is not a regular container class, and does not meet the concept of containers in C++. Therefore, there is no easy way to bypass the elements of the tuple. No cycles are applicable to it:

for (auto& x : tuple_);  // Compilation error!

error: no viable 'begin' function available

You can "walk" through the elements of the tuple only knowing the exact number of its arguments at the compilation stage. To do this, use the get function:

std::get<0>(tuple_); // element index as a template argument

std::get<1>(tuple_);

std::get<2>(tuple_);

In this case, the index transfer during the execution of the program is impossible:

int i = 0;

std::get< i >(tuple_);   // Compilation error!

However, by using template metaprogramming, you can create a more convenient way to bypass the tuple at the compilation stage.

3

Collections of heterogeneous objects of given dimensions.

4

Hash tables (unordered associative containers) unordered_set, unordered_multiset, unordered_map and unordered_multimap.

5

Regular expression library &< regex &>.

6

Smart pointers of general purpose: unique_ptr is added, shared_ptr and weak_ptr are improved, auto_ptr is excluded as obsolete.

7

Extended random number generation