Skip to content

Commit c8cddf8

Browse files
committed
docs: clear notes and add examples
1 parent d7259e7 commit c8cddf8

8 files changed

Lines changed: 312 additions & 355 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ venv
1111
/.idea/editor.xml
1212
/.idea/misc.xml
1313
/.idea/vcs.xml
14+
15+
# TODO: how to ignore executables WITHOUT extension?

‎reference_to_md/cpp/keywords.md‎

Lines changed: 20 additions & 313 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# _identifier_
2+
## := 💡sequence of digits + underscores, lowercase and uppercase Latin letters + MOST Unicode characters💡
3+
* [example.cpp](example.cpp)
4+
## requirements
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <iostream>
2+
3+
int main() {
4+
// 1. with Latin letters
5+
int myVariable = 10;
6+
// 2. case-sensitive
7+
int MyVariable = 20;
8+
int MYVARIABLE = 30; // MYVARIABLE != MyVariable
9+
10+
std::cout << "myVariable: " << myVariable << std::endl;
11+
std::cout << "MyVariable: " << MyVariable << std::endl;
12+
std::cout << "MYVARIABLE: " << MYVARIABLE << std::endl;
13+
14+
// 3. with underscores (`_`)
15+
int my_variable = 40;
16+
int _private = 50;
17+
int __double_underscore = 60;
18+
19+
std::cout << "my_variable: " << my_variable << std::endl;
20+
std::cout << "_private: " << _private << std::endl;
21+
22+
// 4. with digits (NEVER as start)
23+
int var1 = 70;
24+
int variable123 = 80;
25+
int var_2_test = 90;
26+
// int 1var = 100; // ❌ ERROR: cannot start with digit
27+
28+
std::cout << "var1: " << var1 << std::endl;
29+
std::cout << "variable123: " << variable123 << std::endl;
30+
31+
// 5. arbitrarily long identifiers (every character is significant)
32+
int thisIsAVeryLongIdentifierNameThatIsStillValidInCppAndEveryCharacterMatter = 110;
33+
34+
std::cout << "Long identifier 0: " << thisIsAVeryLongIdentifierNameThatIsStillValidInCppAndEveryCharacterMatter << std::endl;
35+
36+
// 6. MOST Unicode characters are ALLOWED (implementation-dependent)
37+
38+
// Common Unicode characters / USUALLY work:
39+
// - Latin extended characters
40+
// - Greek letters
41+
// - Some mathematical symbols
42+
43+
// those / may NOT compile | ALL systems
44+
// int café = 120; // Latin extended
45+
// int α = 130; // Greek alpha
46+
// int β = 140; // Greek beta
47+
48+
// Unicode / characters are NOT ALLOWED
49+
// int 你好 = 150; // ❌ May not work
50+
// int 😀 = 160; // ❌ Emojis are NOT allowed
51+
// int ∑ = 170; // ❌ Many mathematical symbols are NOT allowed
52+
53+
// 7. EVERY character is significant
54+
int temp = 300;
55+
int temp_ = 310;
56+
int _temp = 320;
57+
int temp1 = 330;
58+
int temp_1 = 340;
59+
60+
std::cout << "temp: " << temp << std::endl;
61+
std::cout << "temp_: " << temp_ << std::endl;
62+
std::cout << "_temp: " << _temp << std::endl;
63+
std::cout << "temp1: " << temp1 << std::endl;
64+
std::cout << "temp_1: " << temp_1 << std::endl;
65+
66+
return 0;
67+
}

‎reference_to_md/cpp/language/name.md‎

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
An _identifier_ is an arbitrarily long sequence of digits, underscores, lowercase and uppercase Latin letters, and most Unicode characters.
2-
3-
The first character of a valid identifier must be one of the following:
4-
5-
* uppercase Latin letters A-Z
6-
* lowercase Latin letters a-z
7-
* underscore
8-
* any Unicode character with the Unicode property [XID_Start](https://www.unicode.org/reports/tr31/#Table_Lexical_Classes_for_Identifiers)
9-
10-
11-
12-
Any other character of a valid identifier must be one of the following:
13-
14-
* digits 0-9
15-
* uppercase Latin letters A-Z
16-
* lowercase Latin letters a-z
17-
* underscore
18-
* any Unicode character with the Unicode property [XID_Continue](https://www.unicode.org/reports/tr31/#Table_Lexical_Classes_for_Identifiers)
19-
20-
21-
22-
The lists of characters with properties XID_Start and XID_Continue can be found in [DerivedCoreProperties.txt](https://www.unicode.org/Public/UCD/latest/ucd/DerivedCoreProperties.txt).
23-
24-
Identifiers are case-sensitive (lowercase and uppercase letters are distinct), and every character is significant. Every identifier must conform to [Normalization Form C](https://www.unicode.org/charts/normalization/).
25-
26-
Note: Support of Unicode identifiers is limited in most implementations, e.g. [gcc (until 10)](https://gcc.gnu.org/wiki/FAQ#What_is_the_status_of_adding_the_UTF-8_support_for_identifier_names_in_GCC.3F).
1+
* _identifier_
2+
* := 💡sequence of digits + underscores, lowercase and uppercase Latin letters + MOST Unicode characters💡
3+
* arbitrarily long
4+
* case-sensitive
5+
* == lowercase letterS != uppercase letters
6+
* every character is significant
7+
* MOST Unicode characters
8+
* ❌!= ALL Unicode characters❌
9+
* == limited | MOST implementations
10+
* _Example:_ [gcc 10-](https://gcc.gnu.org/wiki/FAQ#What_is_the_status_of_adding_the_UTF-8_support_for_identifier_names_in_GCC.3F)
11+
* ⚠️requirements⚠️
12+
* MUST conform to [Normalization Form C](https://www.unicode.org/charts/normalization/)
13+
* first character MUST be one of
14+
* uppercase Latin letters A-Z
15+
* lowercase Latin letters a-z
16+
* underscore
17+
* any Unicode character / 's property [XID_Start](https://www.unicode.org/reports/tr31/#Table_Lexical_Classes_for_Identifiers)
18+
* _Example:_ [DerivedCoreProperties.txt](https://www.unicode.org/Public/UCD/latest/ucd/DerivedCoreProperties.txt)
19+
* any other character MUST be one of
20+
* digits 0-9
21+
* uppercase Latin letters A-Z
22+
* lowercase Latin letters a-z
23+
* underscore
24+
* any Unicode character / 's property [XID_Continue](https://www.unicode.org/reports/tr31/#Table_Lexical_Classes_for_Identifiers)
25+
* _Example:_ [DerivedCoreProperties.txt](https://www.unicode.org/Public/UCD/latest/ucd/DerivedCoreProperties.txt)
2726

2827
## Contents
2928

@@ -41,7 +40,7 @@ Note: Support of Unicode identifiers is limited in most implementations, e.g. [g
4140

4241
### In declarations
4342

44-
An identifier can be used [to name](declarations.html "cpp/language/declarations") objects, references, functions, enumerators, types, class members, namespaces, templates, template specializations, parameter packs(since C++11), goto labels, and other entities, with the following exceptions:
43+
An identifier can be used [to name](declarations.md) objects, references, functions, enumerators, types, class members, namespaces, templates, template specializations, parameter packs(since C++11), goto labels, and other entities, with the following exceptions:
4544

4645
* The identifiers that are [keywords](../keywords.html "cpp/keyword") cannot be used for other purposes.
4746

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# syntax
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
// ============================================================================
6+
// (1) Object-like macros: #define identifier replacement-list
7+
// ============================================================================
8+
9+
#define PI 3.14159
10+
#define MAX_SIZE 100
11+
#define VERSION "1.0.0"
12+
#define EMPTY // replacement-list is optional (empty)
13+
14+
void example1_object_like() {
15+
cout << "\n=== (1) Object-like macros ===\n";
16+
cout << "PI = " << PI << "\n";
17+
cout << "MAX_SIZE = " << MAX_SIZE << "\n";
18+
cout << "VERSION = " << VERSION << "\n";
19+
// EMPTY expands to nothing
20+
}
21+
22+
// ============================================================================
23+
// (2) Function-like macros: #define identifier(parameters) replacement-list
24+
// ============================================================================
25+
26+
#define SQUARE(x) ((x) * (x))
27+
#define MAX(a, b) ((a) > (b) ? (a) : (b))
28+
#define MIN(a, b) ((a) < (b) ? (a) : (b))
29+
30+
void example2_function_like() {
31+
cout << "\n=== (2) Function-like macros ===\n";
32+
cout << "SQUARE(5) = " << SQUARE(5) << "\n"; // 25
33+
cout << "MAX(10, 20) = " << MAX(10, 20) << "\n"; // 20
34+
cout << "MIN(10, 20) = " << MIN(10, 20) << "\n"; // 10
35+
}
36+
37+
// ============================================================================
38+
// (3) Variadic macros: #define identifier(parameters, ...) replacement-list
39+
// C++11 - variable number of arguments
40+
// ============================================================================
41+
42+
#define PRINT_NUMS(first, ...) \
43+
cout << "First: " << first << ", Rest: " << #__VA_ARGS__ << "\n"
44+
45+
#define LOG(level, msg) \
46+
cout << "[" << level << "] " << msg << "\n"
47+
48+
void example3_variadic_with_params() {
49+
cout << "\n=== (3) Variadic macros (parameters, ...) ===\n";
50+
PRINT_NUMS(1, 2, 3, 4);
51+
LOG("INFO", "Server started");
52+
LOG("ERROR", "Connection failed");
53+
}
54+
55+
// ============================================================================
56+
// (4) Variadic macros: #define identifier(...) replacement-list
57+
// C++11 - only variadic arguments, no fixed parameters
58+
// ============================================================================
59+
60+
#define PRINT_ALL(...) \
61+
cout << "Arguments: " << #__VA_ARGS__ << "\n"
62+
63+
#define DEBUG(msg) \
64+
cout << "[DEBUG] " << msg << "\n"
65+
66+
void example4_variadic_only() {
67+
cout << "\n=== (4) Variadic macros (...) only ===\n";
68+
PRINT_ALL(1, 2, 3);
69+
PRINT_ALL("hello", "world");
70+
DEBUG("Some debug message");
71+
}
72+
73+
// ============================================================================
74+
// (5) #undef directive
75+
// ============================================================================
76+
77+
#define TEMP_VALUE 999
78+
79+
void example5_undef() {
80+
cout << "\n=== (5) #undef directive ===\n";
81+
cout << "TEMP_VALUE before undef: " << TEMP_VALUE << "\n";
82+
83+
#undef TEMP_VALUE
84+
// TEMP_VALUE is now undefined, cannot use it anymore
85+
// cout << TEMP_VALUE; // ERROR: TEMP_VALUE not defined
86+
87+
cout << "TEMP_VALUE has been undefined\n";
88+
}
89+
90+
// ============================================================================
91+
// OPERATORS: # (stringification) and ## (token pasting)
92+
// ============================================================================
93+
94+
// # operator: converts parameter to string
95+
#define STRINGIFY(x) #x
96+
#define TO_STRING(x) STRINGIFY(x)
97+
98+
// ## operator: concatenates tokens
99+
#define CONCAT(a, b) a##b
100+
#define VAR_NAME(prefix, suffix) prefix##_##suffix
101+
102+
void example6_operators() {
103+
cout << "\n=== # operator (stringification) ===\n";
104+
cout << STRINGIFY(hello world) << "\n"; // "hello world"
105+
cout << TO_STRING(100) << "\n"; // "100"
106+
107+
cout << "\n=== ## operator (token pasting) ===\n";
108+
int value_123 = 456;
109+
cout << "CONCAT(value_, 123) = " << CONCAT(value_, 123) << "\n"; // 456
110+
111+
int my_variable = 789;
112+
cout << "VAR_NAME(my, variable) = " << VAR_NAME(my, variable) << "\n"; // 789
113+
}
114+
115+
// ============================================================================
116+
// C++20: __VA_OPT__
117+
// ============================================================================
118+
119+
#if __cplusplus >= 202002L
120+
#define LOG_WITH_COMMA(msg, ...) \
121+
cout << msg __VA_OPT__(,) __VA_ARGS__ << "\n"
122+
123+
void example7_va_opt() {
124+
cout << "\n=== C++20: __VA_OPT__ ===\n";
125+
LOG_WITH_COMMA("Message"); // No comma if no args
126+
LOG_WITH_COMMA("Message", " extra"); // Comma if args present
127+
}
128+
#endif
129+
130+
// ============================================================================
131+
// Scanning and Replacement example
132+
// ============================================================================
133+
134+
#define EXAMPLE_SCAN 42
135+
#define USE_EXAMPLE EXAMPLE_SCAN
136+
137+
void example8_scanning() {
138+
cout << "\n=== Scanning and Replacement ===\n";
139+
cout << "USE_EXAMPLE = " << USE_EXAMPLE << "\n"; // 42
140+
// Macro expansion: USE_EXAMPLE -> EXAMPLE_SCAN -> 42
141+
}
142+
143+
// ============================================================================
144+
// MAIN
145+
// ============================================================================
146+
147+
int main() {
148+
cout << "=================================================\n";
149+
cout << " #define SYNTAX EXAMPLES\n";
150+
cout << "=================================================\n";
151+
152+
example1_object_like();
153+
example2_function_like();
154+
example3_variadic_with_params();
155+
example4_variadic_only();
156+
example5_undef();
157+
example6_operators();
158+
159+
#if __cplusplus >= 202002L
160+
example7_va_opt();
161+
#else
162+
cout << "\n=== C++20: __VA_OPT__ ===\n";
163+
cout << "(Requires C++20 or later)\n";
164+
#endif
165+
166+
example8_scanning();
167+
168+
cout << "\n=================================================\n";
169+
cout << "Summary:\n";
170+
cout << "(1) Object-like: #define NAME value\n";
171+
cout << "(2) Function-like: #define NAME(params) replacement\n";
172+
cout << "(3) Variadic: #define NAME(params, ...) __VA_ARGS__\n";
173+
cout << "(4) Variadic only: #define NAME(...) __VA_ARGS__\n";
174+
cout << "(5) Undefine: #undef NAME\n";
175+
cout << "Operators: # (stringify), ## (concatenate)\n";
176+
cout << "=================================================\n";
177+
178+
return 0;
179+
}

‎reference_to_md/cpp/preprocessor/replace.md‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
* preprocessor
22
* supports
3-
* text macro replacement
4-
* function-like text macro replacement
3+
* [text macro replacement](#object-like-macros)
4+
* [function-like text macro replacement ](#function-like-macros)
55

66
## Contents
77

@@ -22,6 +22,7 @@
2222
* [7 See also](replace.html#See_also)
2323

2424
### Syntax
25+
* [`identifier`](../language/name.md)
2526
#### (1)
2627
* `#define identifier replacement-list`
2728
* `replacement-list`
@@ -120,20 +121,17 @@ Output:
120121

121122
#### Reserved macro names
122123

123-
A translation unit that [includes a standard library header](../standard_library.html#Including_headers "cpp/standard library") may not #define or #undef names declared in any [standard library header](../headers.html "cpp/header").
124+
* translation unit /
125+
* [includes a standard library header](../standard_library.md#including-headers) may NOT `#define` OR `#undef` names / declared | any [standard library header](../headers.md)
126+
* uses ANY part of the standard library is NOT allowed to `#define` OR `#undef` names lexically identical to
127+
* [keywords](../keywords.md)
128+
* [identifiers / special meaning](../keywords.md)
129+
* | C+11
130+
* [any standard attribute token](../language/attributes.md#standard-attributes)
131+
* | C+11
132+
* EXCEPT TO: [`likely`](../language/attributes/likely.md) & [`unlikely`](../language/attributes/likely.md) defined -- as -- function-like macros
133+
* | C++20
124134

125-
A translation unit that uses any part of the standard library is not allowed to #define or #undef names lexically identical to:
126-
127-
* [keywords](../keywords.html "cpp/keyword")
128-
129-
130-
131-
* [identifiers with special meaning](../keywords.html "cpp/keyword")
132-
* [any standard attribute token](../language/attributes.html#Standard_attributes "cpp/language/attributes"), except that [`likely`](../language/attributes/likely.html "cpp/language/attributes/likely") and [`unlikely`](../language/attributes/likely.html "cpp/language/attributes/likely") may be defined as function-like macros(since C++20)
133-
134-
| (since C++11)
135-
---|---
136-
137135
Otherwise, the behavior is undefined.
138136

139137
#### # and ## operators

0 commit comments

Comments
 (0)