Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Section 75.6: Preprocessor Operators

# operator or stringizing operator is used to convert a Macro parameter to a string literal. It can only be used with the Macros having arguments.

// preprocessor will convert the parameter x to the string literal x #define PRINT(x) printf(#x "\n")

PRINT(This line will be converted to string by preprocessor); // Compiler sees

printf("This line will be converted to string by preprocessor""\n");

Compiler concatenate two strings and the final printf() argument will be a string literal with newline character at its end.

Preprocessor will ignore the spaces before or after the macro argument. So below print statement will give us the same result.

PRINT( This line will be converted to string by preprocessor );

If the parameter of the string literal requires an escape sequence like before a double quote() it will automatically be inserted by the preprocessor.

PRINT(This "line" will be converted to "string" by preprocessor); // Compiler sees

printf("This \"line\" will be converted to \"string\" by preprocessor""\n");

##operator or Token pasting operator is used to concatenate two parameters or tokens of a Macro.

//preprocessor will combine the variable and the x

#define PRINT(x) printf("variable" #x " = %d", variable##x)

int variableY = 15; PRINT(Y); //compiler sees

printf("variable""Y"" = %d", variableY);

and the final output will be

variableY = 15

Section 75.7: #pragma once

Most, but not all, C++ implementations support the #pragma once directive which ensures the file is only included once within a single compilation. It is not part of any ISO C++ standard. For example:

// Foo.h #pragma once

class Foo

{

};

While #pragma once avoids some problems associated with include guards, a #pragma - by definition in the standards - is inherently a compiler-specific hook, and will be silently ignored by compilers that don't support it.

GoalKicker.com – C++ Notes for Professionals

408

Projects which use #pragma once must be modified to be standard-compliant.

With some compilers - particularly those that employ precompiled headers - #pragma once can result in a considerable speedup of the compilation process. Similarly, some preprocessors achieve speedup of compilation by tracking which headers have employed include guards. The net benefit, when both #pragma once and include guards are employed, depends on the implementation and can be either an increase or decrease of compilation times.

#pragma once combined with include guards was the recommended layout for header files when writing MFC based applications on windows, and was generated by Visual Studio’s add class, add dialog, add windows wizards. Hence it is very common to find them combined in C++ Windows Applicants.

Section 75.8: Preprocessor error messages

Compile errors can be generated using the preprocessor. This is useful for a number of reasons some of which include, notifying a user if they are on an unsupported platform or an unsupported compiler.

e.g. Return Error if gcc version is 3.0.0 or earlier.

#if __GNUC__ < 3

#error "This code requires gcc > 3.0.0" #endif

e.g. Return Error if compiling on an Apple computer.

#ifdef __APPLE__

#error "Apple products are not supported in this release" #endif

GoalKicker.com – C++ Notes for Professionals

409