What's the C++ equivalent of #region for C++ so I can put in custom code collapsible bits and make my code a little easier to read?
-
14This is a feature of the IDE rather than the language, isn't it? – Björn Pollex Jan 25 '12 at 09:42
-
4@BjörnPollex, it's used by the IDE but it shouldn't conflict with the language syntax and the compiler must accept it as well. – Darin Dimitrov Jan 25 '12 at 09:43
-
2`#region`, as far as C# goes, is valid syntax and delineates a region of code. Visual Studio allows you to fold them, as an IDE feature. – Thanatos Jan 25 '12 at 09:47
-
10you can't use #region to make your code easier to read, #region makes you code easier to *not* read. – jk. Jan 25 '12 at 11:51
-
I suggest a line comment followed by a block statement. – Grault Jan 24 '14 at 06:34
-
Just using #pragma region and #pragma endregion: https://learn.microsoft.com/en-us/cpp/preprocessor/region-endregion?view=msvc-170 – QMaster Feb 04 '22 at 20:03
11 Answers
The Region keyword is IDE specific and affects rendering in Visual Studio. The nearest equivalent is #pragma Region which is applicable to Visual Studio only .
Code example from MSDN
// pragma_directives_region.cpp
#pragma region Region_1
void Test() {}
void Test2() {}
void Test3() {}
#pragma endregion Region_1
int main() {}

- 2,712
- 2
- 28
- 47

- 20,980
- 8
- 88
- 178
-
if it's only IDE specific should I worry about compiling a code with `#pragma region`? Is there any C compiler which denies it? Is there any consequence at all? Thanks – Ramon Dias Aug 19 '20 at 15:38
-
1Just checked using `Mingw`. Using `gcc -Wall`, shows warnings about unknown pragmas. Like: `warning: ignoring #pragma region test [-Wunknown-pragmas]` and `warning: ignoring #pragma endregion [-Wunknown-pragmas]` – Ramon Dias Aug 19 '20 at 15:47
In addition to #pragma region
…#pragma endregion
for Visual Studio, many IDEs support the following syntax for regions in any {}
-delimited, //
-commented language:
//{ Region header text.
…
//}
Notable examples include Code::Blocks and FlashDevelop, and any other editor that uses the Scintilla editing component, such as Notepad++, Geany, Komodo Edit, and many more.

- 53,300
- 8
- 96
- 166
-
-
1
-
I would suggest adding the header before the opening brace so that when it's collapsed you'll still be able to see the name (in more compilers at least). – Emad Y Nov 27 '16 at 11:41
There isn't an equivalent in C++. However IDEs should be able to collapse sections.
It is also possible to use something like this:
#pragma region
#pragma endregion A comment about the region.
But probably not very portable

- 3,685
- 3
- 35
- 75
I've been using
#ifndef ANY_NAME_FOR_THIS_REGION
...
#endif
for several projects during the last couple of years and that suits me (including collapsible blocks). as an addition, i can disable the block using #define ANY_NAME_FOR_THIS_REGION just above it.

- 327
- 4
- 7
-
1#if 1 is even simpler than #ifndef, but.. it doesnt enforce the use of a symbol to describe the purpose of that region (the name following #ifndef) - I know you can prepend or append a comment to "#if 1", but it's not managed by the IDE. To be honest, i still use "#if 1..else..endif" just when i need to execute just one of two blocks of code (one block XOR the other) – Drout Aug 09 '17 at 21:50
There is no equivalent. The #region
feature is part of the C# specification.
C++ has no such equivalent. You could possibly mimic it with specially formatted comments, but this would be editor specific.
For Visual Studio you can use:
#pragma region name
...
#pragma endregion name

- 30,738
- 21
- 105
- 131

- 489,969
- 99
- 883
- 1,009
-
1So when you say *there is no such equivalent* you are wrong because `#pragma region` _is_, for all intents and purposes, its functional equivalent. – Dmitri Nesteruk Oct 04 '14 at 17:49
-
9@DmitriNesteruk - no, it isn't - not if you use any IDE that is not Visual Studio. `#region` is defined in the spec and IDEs that conform to the spec should allow collapsing regions - this can't be said for `#pragma region`. – Oded Oct 04 '14 at 18:22
Just an addition to other answers. The region definition varies from IDE to IDE.
For Mac development in Xcode you can use a pragma:
#pragma mark

- 3,600
- 8
- 61
- 86

- 14,289
- 3
- 40
- 55
C++Builder does support this, but you must declare the region as:
#pragma region BLAH
.....
#pragma end_region
You must use end_region for C++Builder, but it will work, and it will collapse the region!

- 30,738
- 21
- 105
- 131

- 11
- 1
Kate, KDevelop and all other text editors and IDEs which use Katepart supports marking regions with //BEGIN
and //END
markers.
// BEGIN GPT entity types
#define GPT_ENT_TYPE_UNUSED \
{0x00000000,0x0000,0x0000,0x00,0x00,{0x00,0x00,0x00,0x00,0x00,0x00}}
#define GPT_ENT_TYPE_EFI \
{0xc12a7328,0xf81f,0x11d2,0xba,0x4b,{0x00,0xa0,0xc9,0x3e,0xc9,0x3b}}
#define GPT_ENT_TYPE_MBR \
{0x024dee41,0x33e7,0x11d3,0x9d,0x69,{0x00,0x08,0xc7,0x81,0xf3,0x9f}}
// END
You will be able to collapse a region defined in such way.

- 404
- 3
- 14
I use multiple blocks of namespace'd code with the same namespace. The IDE let's me collapse by namespace block, so I just see the code of single block I'm working on e.g.
namespace MyNamespace{ // Quantise utility
int Quantise1() { ...}
float Quantise2() { ...}
} // MyNamespace Quantise utility
namespace MyNamespace{ // ADC utility
int ADC1() { ...}
float ADC2() { ...}
} // MyNamespace ADC utility

- 1,181
- 11
- 17
There is no equivalent.
Most good editors or IDEs will let you collapse functions, if not also if
/else
/while
/for
/etc.

- 42,585
- 14
- 91
- 146
The first answer from this question mentions another alternative. It is not applicable in all situations, though.
Method: Use {...} instead which natively supports code collapsing in Visual Studio.
Enable option: Tools -> Options -> Text Editor -> C/C++ -> Formatting -> OutLine Statement Blocks -> True.
Put your in different scopes {...}, then it will collapse the code in different scopes:
-
7This might not work with all code as it will cause scoping issues for variables inside this block – asami Dec 17 '14 at 05:26