前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则​NL.17:使用K&R风格派生的布局

C++核心准则​NL.17:使用K&R风格派生的布局

作者头像
面向对象思考
发布2020-12-15 15:00:10
6290
发布2020-12-15 15:00:10
举报

NL.17: Use K&R-derived layout

NL.17:使用K&R风格派生的布局

Reason(原因)

This is the original C and C++ layout. It preserves vertical space well. It distinguishes different language constructs (such as functions and classes) well.

这是原始的C和C ++布局。它很好地保留了垂直空间。它很好地区分了不同的语言构造(例如函数和类)。

Note(注意)

In the context of C++, this style is often called "Stroustrup".

在C ++的上下文中,此样式通常称为“斯特劳斯特卢普”。

This is a recommendation for when you have no constraints or better ideas. This rule was added after many requests for guidance.

当您没有约束或更好的选择时,可以考虑这个个建议。此规则可以在许多准则之后适用。

Example(示例)

代码语言:javascript
复制
struct Cable {
    int x;
    // ...
};

double foo(int x)
{
    if (0 < x) {
        // ...
    }

    switch (x) {
    case 0:
        // ...
        break;
    case amazing:
        // ...
        break;
    default:
        // ...
        break;
    }

    if (0 < x)
        ++x;

    if (x < 0)
        something();
    else
        something_else();

    return some_value;
}

Note the space between if and (

注意if和(之间的空格

Note(注意)

Use separate lines for each statement, the branches of an if, and the body of a for.

对于每个语句,if的分支和for的正文使用单独的行。

Note(注意)

The { for a class and a struct is not on a separate line, but the { for a function is.

类和结构的{不在单独的行上,而函数的{是。

Note(注意)

Capitalize the names of your user-defined types to distinguish them from standards-library types.

大写用户定义类型的名称,以将其与标准库类型区分开。

Note(注意)

Do not capitalize function names.

不要大写函数名称。

Enforcement(实施建议)

If you want enforcement, use an IDE to reformat.

如果要强制执行,请使用IDE重新格式化。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#nl17-use-kr-derived-layout

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-11-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • NL.17: Use K&R-derived layout
  • Reason(原因)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档