Equivalent of #region for C++

2020-05-18 04:32发布

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# c++
10条回答
够拽才男人
2楼-- · 2020-05-18 04:44

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
查看更多
叛逆
3楼-- · 2020-05-18 04:45

There is no equivalent.

Most good editors or IDEs will let you collapse functions, if not also if/else/while/for/etc.

查看更多
叼着烟拽天下
4楼-- · 2020-05-18 04:46

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!

查看更多
放荡不羁爱自由
5楼-- · 2020-05-18 04:55

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.

  1. Enable option: Tools -> Options -> Text Editor -> C/C++ -> Formatting -> OutLine Statement Blocks -> True.

  2. Put your in different scopes {...}, then it will collapse the code in different scopes:

Scoped code collapsing example

查看更多
姐就是有狂的资本
6楼-- · 2020-05-18 04:59

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.

查看更多
Bombasti
7楼-- · 2020-05-18 04:59

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.

查看更多
登录 后发表回答