C# 相当于 C++ 的 #region

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9000479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:31:36  来源:igfitidea点击:

Equivalent of #region for C++

c#c++

提问by SirYakalot

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++ 中#region 的C++ 等价物是什么,以便我可以放入自定义代码可折叠位并使我的代码更易于阅读?

采纳答案by Dr. ABT

The Region keywordis IDE specific and affects rendering in Visual Studio. The nearest equivalent is #pragma Regionwhich is applicable to Visual Studio only .

地区的关键字是IDE具体和Visual Studio中会影响渲染。最接近的等效项是#pragma Region,它仅适用于 Visual Studio。

Code example from MSDN

来自MSDN 的代码示例

// pragma_directives_region.cpp
#pragma region Region_1
void Test() {}
void Test2() {}
void Test3() {}
#pragma endregion Region_1

int main() {}

回答by Oded

There is no equivalent. The #regionfeatureis part of the C# specification.

没有等价物。该#region功能是 C# 规范的一部分。

C++ has no such equivalent. You could possibly mimic it with specially formatted comments, but this would be editor specific.

C++ 没有这样的等价物。您可以使用特殊格式的注释来模仿它,但这将是特定于编辑器的。

For Visual Studio you can use:

对于 Visual Studio,您可以使用:

#pragma region name
...
#pragma endregion name

回答by Thanatos

There is no equivalent.

没有等价物。

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

大多数优秀的编辑器或 IDE 都可以让您折叠功能,如果不能,也可以if/ else/ while/ for/ 等。

回答by Firedragon

There isn't an equivalent in C++. However IDEs should be able to collapse sections.

C++ 中没有等价物。但是 IDE 应该能够折叠部分。

It is also possible to use something like this:

也可以使用这样的东西:

#pragma region

#pragma endregion A comment about the region.

But probably not very portable

但可能不是很便携

回答by Jon Purdy

In addition to #pragma region#pragma endregionfor Visual Studio, many IDEs support the following syntax for regions in any {}-delimited, //-commented language:

除了#pragma region……#pragma endregion对于 Visual Studio,许多 IDE 支持以下任何{}-delimited、//-commented 语言区域的语法:

//{ Region header text.
…
//}

Notable examples include Code::Blocksand FlashDevelop, and any other editor that uses the Scintillaediting component, such as Notepad++, Geany, Komodo Edit, and many more.

值得注意的示例包括Code::BlocksFlashDevelop,以及任何其他使用Scintilla编辑组件的编辑器,例如Notepad++GeanyKomodo Edit等等。

回答by Viktor Latypov

Just an addition to other answers. The region definition varies from IDE to IDE.

只是对其他答案的补充。区域定义因 IDE 而异。

For Mac development in Xcodeyou can use a pragma:

对于Xcode 中的Mac 开发,您可以使用编译指示:

#pragma mark

回答by Selmar

The first answer from this questionmentions another alternative. It is not applicable in all situations, though.

这个问题的第一个答案提到了另一种选择。但是,它并不适用于所有情况。

Method:Use {...} instead which natively supports code collapsing in Visual Studio.

方法:使用 {...} 代替它本身支持 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:

  1. 启用选项:工具 -> 选项 -> 文本编辑器 -> C/C++ -> 格式 -> OutLine 语句块 -> True。

  2. 把你的在不同的范围内{...},然后它会在不同的范围内折叠代码:

Scoped code collapsing example

范围代码折叠示例

回答by user4351871

C++Builderdoessupport this, but you must declare the region as:

C++Builder确实支持这一点,但您必须将区域声明为:

#pragma region BLAH

.....

#pragma end_region

You mustuse end_region for C++Builder, but it willwork, and it willcollapse the region!

必须对 C++Builder 使用 end_region,但它起作用,并且折叠区域!

回答by Drout

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.

过去几年的几个项目,这适合我(包括可折叠块)。另外,我可以使用它上方的 #define ANY_NAME_FOR_THIS_REGION 禁用该块。

回答by Sauron

Kate, KDevelop and all other text editors and IDEs which use Katepartsupports marking regions with //BEGINand //ENDmarkers.

Kate、KDevelop 和所有其他使用Katepart 的文本编辑器和 IDE支持使用//BEGIN//END标记标记区域。

// 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.

您将能够折叠以这种方式定义的区域。