Revision history for KeyPgPpdefine


Revision [21083]

Last edited on 2016-03-13 09:14:33 by fxm [Formatting]

No Differences

Revision [20448]

Edited on 2016-02-10 16:06:40 by DkLwikki [Update link format]
Additions:
#define //identifier//( [ //parameters//, ] //[[KeyPgDots|variadic_parameter...]]// ) //body//
##**#define**## allows to declare text-based preprocessor macros. Once the compiler has seen a ##**#define**##, it will start replacing further occurrences of ##//identifier//## with ##//body//##. ##//body//## may be empty. The expansion is done recursively, until there is nothing more to expand and the compiler can continue analyzing the resulting code. ##[[KeyPgPpundef|#undef]]## can be used to make the compiler forget about a ##**#define**##.
##//Parameters//## turn a define into a function-like macro, allowing text arguments to be passed to the macro. Any occurrences of the parameter names in the ##//body//## will be replaced by the given argument text during expansion. The ##[[KeyPgOpPpStringize|# stringize]]## operator can be used on macro parameters to turn them into string literals, and the ##[[KeyPgOpPpConcat|## concatenate]]## operator can be used to merge tokens together.
Defines are //scoped//; they are only visible in the scope they were defined in. If defined at module level, the define is visible throughout the module. If the ##//identifier//## is defined inside a compound statement having scope (##[[KeyPgSub|Sub]]##, ##[[KeyPgFornext|For..Next]]##, ##[[KeyPgWhilewend|While..Wend]]##, ##[[KeyPgDoloop|Do..Loop]]##, ##[[KeyPgScope|Scope..End Scope]]##, etc), the ##//identifier//## define is only visible within that scope. Namespaces on the other hand do not have any effect on the visibility of a define.
##//Identifier//##s can be checked for with ##[[KeyPgPpifdef|#ifdef]]## and others, which can be used to hide parts of code from the compiler (conditional compiling).
The result of macro expansion can be checked by using the [[CompilerOptpp|-pp]] compiler option.
##**#define**##s are often used to declare constants. The ##[[KeyPgConst|Const]]## statement is a type-safe alternative.
- ##[[KeyPgPpmacro|#macro]]##
- ##[[KeyPgOpPpStringize|# Preprocessor stringize]]##
- ##[[KeyPgOpPpConcat|## Preprocessor concatenate]]##
- ##[[KeyPgPpifdef|#ifdef]]##
- ##[[KeyPgPpundef|#undef]]##
- ##[[KeyPgConst|Const]]##
- ##[[KeyPgDots|...]]##
Deletions:
#define //identifier//( [ //parameters//, ] //[[KeyPgDots variadic_parameter...]]// ) //body//
##**#define**## allows to declare text-based preprocessor macros. Once the compiler has seen a ##**#define**##, it will start replacing further occurrences of ##//identifier//## with ##//body//##. ##//body//## may be empty. The expansion is done recursively, until there is nothing more to expand and the compiler can continue analyzing the resulting code. ##[[KeyPgPpundef #undef]]## can be used to make the compiler forget about a ##**#define**##.
##//Parameters//## turn a define into a function-like macro, allowing text arguments to be passed to the macro. Any occurrences of the parameter names in the ##//body//## will be replaced by the given argument text during expansion. The ##[[KeyPgOpPpStringize # stringize]]## operator can be used on macro parameters to turn them into string literals, and the ##[[KeyPgOpPpConcat ## concatenate]]## operator can be used to merge tokens together.
Defines are //scoped//; they are only visible in the scope they were defined in. If defined at module level, the define is visible throughout the module. If the ##//identifier//## is defined inside a compound statement having scope (##[[KeyPgSub Sub]]##, ##[[KeyPgFornext For..Next]]##, ##[[KeyPgWhilewend While..Wend]]##, ##[[KeyPgDoloop Do..Loop]]##, ##[[KeyPgScope Scope..End Scope]]##, etc), the ##//identifier//## define is only visible within that scope. Namespaces on the other hand do not have any effect on the visibility of a define.
##//Identifier//##s can be checked for with ##[[KeyPgPpifdef #ifdef]]## and others, which can be used to hide parts of code from the compiler (conditional compiling).
The result of macro expansion can be checked by using the [[CompilerOptpp -pp]] compiler option.
##**#define**##s are often used to declare constants. The ##[[KeyPgConst Const]]## statement is a type-safe alternative.
- ##[[KeyPgPpmacro #macro]]##
- ##[[KeyPgOpPpStringize # Preprocessor stringize]]##
- ##[[KeyPgOpPpConcat ## Preprocessor concatenate]]##
- ##[[KeyPgPpifdef #ifdef]]##
- ##[[KeyPgPpundef #undef]]##
- ##[[KeyPgConst Const]]##
- ##[[KeyPgDots ...]]##


Revision [19866]

Edited on 2016-02-10 08:45:41 by fxm [Rewording of the warning note]
Additions:
WARNING: When the macro body contains an expression with one operator at least, it may be mandatory to have to surround some terms (parameters, whole body) by parentheses in order to not undergo an unwanted precedence change of operators.
Deletions:
WARNING: When the macro body contains an expression with one operator at least, it may be mandatory to have to surround some terms (parameters, whole body) by parentheses in order not to undergo an unwanted precedence of operators.


Revision [19864]

Edited on 2016-02-10 02:58:02 by fxm [Added warning versus operator precedence]
Additions:
WARNING: When the macro body contains an expression with one operator at least, it may be mandatory to have to surround some terms (parameters, whole body) by parentheses in order not to undergo an unwanted precedence of operators.
Deletions:
WARNING: When the macro body contains an expression with at least one operator, it may be mandatory to surround some terms with parentheses in order to not undergo an unwanted operator precedence.


Revision [19863]

Edited on 2016-02-09 16:24:22 by fxm [Added warning versus operator precedence]
Additions:
WARNING: When the macro body contains an expression with at least one operator, it may be mandatory to surround some terms with parentheses in order to not undergo an unwanted operator precedence.


Revision [16894]

Edited on 2013-07-24 14:44:10 by CountingPine [Give note about '()' parameter list its own separated paragraph]

No Differences

Revision [15309]

Edited on 2011-10-01 09:44:08 by DkLwikki [Rewrote a bit for more information]
Additions:
#define //identifier// //body//
#define //identifier//( [ //parameters// ] ) //body//
#define //identifier//( [ //parameters//, ] //[[KeyPgDots variadic_parameter...]]// ) //body//
##**#define**## allows to declare text-based preprocessor macros. Once the compiler has seen a ##**#define**##, it will start replacing further occurrences of ##//identifier//## with ##//body//##. ##//body//## may be empty. The expansion is done recursively, until there is nothing more to expand and the compiler can continue analyzing the resulting code. ##[[KeyPgPpundef #undef]]## can be used to make the compiler forget about a ##**#define**##.
##//Parameters//## turn a define into a function-like macro, allowing text arguments to be passed to the macro. Any occurrences of the parameter names in the ##//body//## will be replaced by the given argument text during expansion. The ##[[KeyPgOpPpStringize # stringize]]## operator can be used on macro parameters to turn them into string literals, and the ##[[KeyPgOpPpConcat ## concatenate]]## operator can be used to merge tokens together.
Note: In the function-like macro declaration, the ##//identifier//## should be followed by the opening parentheses (##(##) immediately without any white-space in between, otherwise the compiler will treat it as part of the ##//body//##.
Defines are //scoped//; they are only visible in the scope they were defined in. If defined at module level, the define is visible throughout the module. If the ##//identifier//## is defined inside a compound statement having scope (##[[KeyPgSub Sub]]##, ##[[KeyPgFornext For..Next]]##, ##[[KeyPgWhilewend While..Wend]]##, ##[[KeyPgDoloop Do..Loop]]##, ##[[KeyPgScope Scope..End Scope]]##, etc), the ##//identifier//## define is only visible within that scope. Namespaces on the other hand do not have any effect on the visibility of a define.
##//Identifier//##s can be checked for with ##[[KeyPgPpifdef #ifdef]]## and others, which can be used to hide parts of code from the compiler (conditional compiling).
The result of macro expansion can be checked by using the [[CompilerOptpp -pp]] compiler option.
##**#define**##s are often used to declare constants. The ##[[KeyPgConst Const]]## statement is a type-safe alternative.
'' Function-like definition
- ##[[KeyPgDots ...]]##
Deletions:
#define //identifier// //text//
#define //identifier//( [ //parameters// ] ) //macro_text//
#define //identifier//( [ //parameters//, ] //variadic_parameter//[[KeyPgDots ...]] ) //macro_text//
Preprocessor keyword that defines an identifier with a custom meaning:
- Non-empty defines (with ##//text//##) are substituted by its ##//text//## when the source is parsed, allowing a sort of "shorthand". ##//text//## may be empty, which is useful for making defines that are just designed for checking with ##[[KeyPgPpifdef #ifdef]]## and ##[[KeyPgPpifndef #ifndef]]##.
- Defines with parameters are substituted by the ##//macro_text//##, that will contain all the arguments passed replaced. **Note:** The open parentheses character ("##(##") must immediately follow the ##//identifier//##, there should be no white-spaces between them, otherwise the parentheses will be taken as part of ##//text//##.
- Defines are visible only in the scope where they are defined. If defined at module level, the define is visible throughout the module. If the ##//identifier//## is defined inside a compound statement having scope (##[[KeyPgSub Sub]]##, ##[[KeyPgFornext For..Next]]##, ##[[KeyPgWhilewend While..Wend]]##, ##[[KeyPgDoloop Do..Loop]]##, ##[[KeyPgScope Scope..End Scope]]##, etc), the ##//identifier//## is visible only within that scope.
- Namespaces do not have any effect on the visibility of a define.
##//Identifier//##s can be checked to see whether they have been defined with ##[[KeyPgPpifdef #ifdef]]## and ##[[KeyPgPpifndef #ifndef]]##, which can be used to hide parts of code to the compiler (conditional compiling).
For defining identifiers with constant values associated with them ##[[KeyPgConst Const]]## may be used as a more powerful method.
Using ##[[KeyPgDots ...]]## (an ellipsis) behind the last parameter of a macro allows to create a variadic macro. This means it is possible to pass any number of arguments to the //variadic_parameter//, which can be used in the //macro_text//, just like a normal macro parameter. The //variadic_parameter// will expand to the full list of arguments passed to it, including commas, and can also be completely empty.
'' Function like definition
{{fbdoc item="filename" value="examples/manual/prepro/variadic.bas"}}%%(freebasic)
'' Using a variadic macro to wrap a variadic function
#include "crt.bi"
#define eprintf(format, args...) fprintf(stderr, format, args)
eprintf(!"Hello from printf: %i %s %i\n", 5, "test", 123)
'' LISP-like accessors allowing to modify comma-separated lists
#define car(a, b...) a
#define cdr(a, b...) b
- ##[[KeyPgPpifndef #ifndef]]##
- ##[[KeyPgDefined defined]]##


Revision [14862]

Edited on 2010-10-12 08:37:30 by DkLwikki [Added variadic macros]
Additions:
#define //identifier//( [ //parameters// ] ) //macro_text//
#define //identifier//( [ //parameters//, ] //variadic_parameter//[[KeyPgDots ...]] ) //macro_text//

Using ##[[KeyPgDots ...]]## (an ellipsis) behind the last parameter of a macro allows to create a variadic macro. This means it is possible to pass any number of arguments to the //variadic_parameter//, which can be used in the //macro_text//, just like a normal macro parameter. The //variadic_parameter// will expand to the full list of arguments passed to it, including commas, and can also be completely empty.
{{fbdoc item="filename" value="examples/manual/prepro/variadic.bas"}}%%(freebasic)
'' Using a variadic macro to wrap a variadic function
#include "crt.bi"
#define eprintf(format, args...) fprintf(stderr, format, args)
eprintf(!"Hello from printf: %i %s %i\n", 5, "test", 123)
'' LISP-like accessors allowing to modify comma-separated lists
#define car(a, b...) a
#define cdr(a, b...) b
Deletions:
#define //identifier//([//parameters//]) //macro_text//


Revision [14095]

Edited on 2009-01-17 13:16:25 by CountingPine [Change some details, take out some less directly-related See Alsos.]
Additions:
- Non-empty defines (with ##//text//##) are substituted by its ##//text//## when the source is parsed, allowing a sort of "shorthand". ##//text//## may be empty, which is useful for making defines that are just designed for checking with ##[[KeyPgPpifdef #ifdef]]## and ##[[KeyPgPpifndef #ifndef]]##.
- Defines with parameters are substituted by the ##//macro_text//##, that will contain all the arguments passed replaced. **Note:** The open parentheses character ("##(##") must immediately follow the ##//identifier//##, there should be no white-spaces between them, otherwise the parentheses will be taken as part of ##//text//##.
##//Identifier//##s can be checked to see whether they have been defined with ##[[KeyPgPpifdef #ifdef]]## and ##[[KeyPgPpifndef #ifndef]]##, which can be used to hide parts of code to the compiler (conditional compiling).
For defining identifiers with constant values associated with them ##[[KeyPgConst Const]]## may be used as a more powerful method.
- New to ""FreeBASIC""
- ##[[KeyPgOpPpStringize # Preprocessor stringize]]##
- ##[[KeyPgOpPpConcat ## Preprocessor concatenate]]##
- ##[[KeyPgConst Const]]##
Deletions:
#define //identifier//
- Empty defines (without //text//) can be checked for existence with ##[[KeyPgPpifdef #ifdef]]## and ##[[KeyPgPpifndef #ifndef]]## to hide parts of code to the compiler (conditional compiling).
- Non-empty defines (with //text//) are substituted by its //text// when the source is parsed, allowing a sort of "shorthand".
- Defines with parameters are substituted by the //macro_text//, that will contain all the arguments passed replaced. **Note:** The open parentheses character ('(') must immediately follow the //identifier//, there should be no white-spaces between them.
- New to FreeBASIC
- ##[[KeyPgPpif #if]]##
- ##[[KeyPgPpelse #else]]##
- ##[[KeyPgPpelseif #elseif]]##
- ##[[KeyPgPpendif #endif]]##


Revision [12495]

The oldest known version of this page was created on 2008-01-19 09:42:52 by JeffMarshall [Change some details, take out some less directly-related See Alsos.]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode