Online Book Reader

Home Category

Professional C__ - Marc Gregoire [152]

By Root 1190 0
For example:

'a': character

"character array": zero-terminated array of characters, C-style string

3.14f: float floating point value

0xabc: hexadecimal value

C++11 allows you to define your own literals. These user defined literals should start with an underscore and are implemented by writing literal operators. A literal operator can work in raw or cooked mode. In raw mode, your literal operator will receive a sequence of characters, while in cooked mode your literal operator will receive a specific interpreted type. For example, take the C++ literal 123. Your raw literal operator will receive this as a sequence of characters '1', '2', '3'. Your cooked literal operator will receive this as the integer 123. Another example, take the C++ literal 0x23. The raw operator will receive the characters '0', 'x', '2', '3', while the cooked operator will receive the integer 35. One last example, take the C++ literal 3.14. Your raw operator will receive this as '3', '.', '1', '4', while your cooked operator will receive the floating point value 3.14.

A cooked mode literal operator should have:

one parameter of type unsigned long long or long double to process numeric values,

or two parameters where the first is a character array and the second is the length of the character array, to process strings. For example: const char* str, size_t len.

As an example, the following implements a cooked literal operator for the user defined literal _i to define a complex number literal.

std::complex operator"" _i(long double d)

{

return std::complex(0, d);

}

Code snippet from UserDefinedLiterals\UserDefinedLiterals.cpp

This _i literal can be used as follows:

std::complex c1 = 9.634_i;

auto c2 = 1.23_i; // c2 will have as type std::complex

Code snippet from UserDefinedLiterals\UserDefinedLiterals.cpp

A second example implements a cooked operator for a user defined literal _s to define std::string literals:

std::string operator"" _s(const char* str, size_t len)

{

return std::string(str, len);

}

Code snippet from UserDefinedLiterals\UserDefinedLiterals.cpp

This literal can be used as follows:

std::string str1 = "Hello World"_s;

auto str2 = "Hello World"_s; // str2 will have as type std::string

Code snippet from UserDefinedLiterals\UserDefinedLiterals.cpp

Without the _s literal, the auto type deduction would be const char*:

auto str3 = "Hello World"; // str3 will have as type const char*

Code snippet from UserDefinedLiterals\UserDefinedLiterals.cpp

A raw mode literal operator requires one parameter of type const char*; a zero-terminated C-style string. The following example defines the literal _i but using a raw literal operator:

std::complex operator"" _i(const char* p)

{

// Implementation omitted; it requires parsing the C-style

// string and converting it to a complex number.

}

Code snippet from UserDefinedLiterals\UserDefinedLiterals.cpp

Using this raw mode operator is exactly the same as using the cooked version:

std::complex c1 = 9.634_i;

auto c2 = 1.23_i; // c2 will have as type std::complex

Some more examples of useful user defined literals that you could implement:

1000101110_b: a binary number

10_s: seconds

100_km: kilometer

and so on...

HEADER FILES


Header files are a mechanism for providing an abstract interface to a subsystem or piece of code. One of the trickier parts of using headers is avoiding circular references and multiple includes of the same header file. For example, perhaps you are responsible for writing the Logger class that performs all error message logging tasks. You may end up using another class, Preferences, that keeps track of user settings. The Preferences class may in turn use the Logger class indirectly, through yet another header.

As the following code shows, the #ifndef mechanism can be used to avoid circular and multiple includes. At the beginning of each header file, the #ifndef directive checks to see if a certain key has not been defined. If the key has been defined, the compiler

Return Main Page Previous Page Next Page

®Online Book Reader