Online Book Reader

Home Category

Professional C__ - Marc Gregoire [151]

By Root 1129 0
over all the elements in the given list and to accumulate the total sum.

This function can be used as follows.

int a = makeSum({1,2,3});

int b = makeSum({10,20,30,40,50,60});

Initializer lists are type-safe and define which type is allowed to be in the list. For the above makeSum() function, calling it with a double value is invalid:

int c = makeSum({1,2,3.0});

The last element is a double, which will result in a compiler error.

Explicit Conversion Operators

Chapter 7 discusses the implicit conversion that can happen with single argument constructors and how to prevent the compiler from using those implicit conversions with the explicit keyword. The C++ compiler will also perform implicit conversion with custom written conversion operators. In C++11 it is now possible to apply the explicit keyword, not only to constructors, but also to conversion operators.

To explain explicit conversion operators, you need to understand implicit conversion first. Take the following example. It defines a class IntWrapper that just wraps an integer and implements an int() conversion operator, which the compiler can use to perform implicit conversion from an IntWrapper to type int.

class IntWrapper

{

public:

IntWrapper(int i) : mInt(i) {}

operator int() const { return mInt; }

private:

int mInt;

};

Code snippet from ExplicitConversionOperators\ExplicitConversionOperators.cpp

The following code demonstrates this implicit conversion; iC1 will contain the value 123.

IntWrapper c(123);

int iC1 = c;

Code snippet from ExplicitConversionOperators\ExplicitConversionOperators.cpp

If you want, you can still explicitly tell the compiler to call the int() conversion operator as follows. iC2 will also contain the value 123.

int iC2 = int(c);

Code snippet from ExplicitConversionOperators\ExplicitConversionOperators.cpp

With C++11, you can use the explicit keyword to prevent the compiler from performing the implicit conversion. Below is the new class definition.

class IntWrapper

{

public:

IntWrapper(int i) : mInt(i) {}

explicit operator int() const { return mInt; }

private:

int mInt;

};

Code snippet from ExplicitConversionOperators\ExplicitConversionOperators.cpp

Trying to compile the following lines of code with this new class definition will result in a compiler error because the int() conversion operator is marked as explicit, so the compiler cannot use it anymore to perform implicit conversion.

IntWrapper c(123);

int iC1 = c; // Error, because of explicit int() operator

Code snippet from ExplicitConversionOperators\ExplicitConversionOperators.cpp

Once you have an explicit conversion operator, you have to explicitly call it if you want to use it. For example:

int iC2 = int(c);

int iC3 = static_cast(c);

Code snippet from ExplicitConversionOperators\ExplicitConversionOperators.cpp

Attributes

Attributes are a new mechanism to add optional and/or vendor-specific information into source code. Before C++11, the vendor decided how to specify that information. Examples are __attribute__, __declspec, and so on. C++11 adds support for attributes by using the double square brackets syntax [[attribute]].

The C++11 standard defines only two standard attributes: [[noreturn]] and [[carries_dependency]].

[[noreturn]] means that a function never returns control to the call site. Typically, the function either causes some kind of termination (process termination or thread termination) or throws an exception. For example:

[[noreturn]] void func()

{

throw 1;

}

The second attribute, [[carries_dependency]], is a rather exotic attribute and is not discussed further.

Most attributes will be vendor-specific extensions. The C++11 standard advises vendors to not use attributes to change the meaning of the program, but to use them to help the compiler to optimize code or detect errors in code. Since attributes of different vendors could clash, vendors are recommended to qualify them. For example:

[[microsoft::novtable]]

User Defined Literals

C++ has a number of standard literals that you can use in your code.

Return Main Page Previous Page Next Page

®Online Book Reader