Equivalent of #region for C++
C#C++C# Problem Overview
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?
C# Solutions
Solution 1 - C#
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() {}
Solution 2 - C#
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.
Solution 3 - C#
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
Solution 4 - C#
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
Solution 5 - C#
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.
Solution 6 - C#
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
Solution 7 - C#
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!
Solution 8 - C#
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.
Solution 9 - C#
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
Solution 10 - C#
There is no equivalent.
Most good editors or IDEs will let you collapse functions, if not also if
/else
/while
/for
/etc.
Solution 11 - C#
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: