在C ++中的init列表后的大括号 [英] Brace placement after init list in C++

查看:188
本文介绍了在C ++中的init列表后的大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于任何有大括号的人:

For anyone who places braces thus:

void f() {
    stuff();
}

如何在长初始化列表后放置大括号?

同样吗?

How do you prefer to place braces after long initializer lists?
The same way?

Object::Object()
    : foo(1)
    , bar(2) {
    stuff();
}

或者创建一个异常, p>

Or make an exception so you actually see where the init list ends?

Object::Object()
    : foo(1)
    , bar(2)
{
    stuff();
}

还是留空行?

Object::Object()
    : foo(1)
    , bar(2) {

    stuff();
}

还是做一个奇怪的杂种?

Or maybe make a weird hybrid?

Object::Object()
    : foo(1)
    , bar(2)
    {
    stuff();
}

或滥用缩图

Object::Object()
: foo(1)
, bar(2) {
    stuff();
}

Object::Object() : foo(1)
                            , bar(2) {
    stuff();
}

在这个小例子中,所有的都是漂亮的,函数体,并且这快速变化。

In this small example all are pretty but crank a dozen initializers and a moderately long function body and this quickly changes.

推荐答案

看起来重要的是能够检测ctor初始化列表的开始/代码块的开始/结束位置。

It seems important to be able to detect where the ctor-initializer list begins/ends, and where the code block begins/ends.

Object::Object ()
  : foo (1)
  , bar (2)
{
  ...
}

这篇关于在C ++中的init列表后的大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆