【问题标题】:Where is the coding style of cppreference defined?cppreference的编码风格在哪里定义?
【发布时间】:2020-03-20 08:04:55
【问题描述】:

我在 cppreference 上看到了很多示例代码。 例如,以下 URL 有一个代码。

https://en.cppreference.com/w/cpp/language/list_initialization

从上面的例子中,我们可以观察到大括号的缩进在structfunction 上是不同的,如下所示。

struct Foo { // left-brace is on the same line with the name of the struct 
    std::vector<int> mem = {1, 2, 3}; // default indent seems 4 spaces
    std::vector<int> mem2;
    Foo() : mem2{-1, -2, -3} {}
}; // right-brace starts with a new line

std::pair<std::string, std::string> f(std::pair<std::string, std::string> p)
{ // left-brace starts with a new line for function
    return {p.second, p.first}; // list-initialization in return statement
} // right-brace starts with a new line for function

int main()
{ // same as above
 //...
} // same as above

编码风格在哪里描述?

【问题讨论】:

  • 这叫Stroustrup风格

标签: c++ coding-style std


【解决方案1】:

cppreference上的样式描述@Help:Manual of style(Code formatting)

对于间距和缩进,使用K&R variant

如果函数的参数跨越多行,则所有参数的缩进与左括号匹配。模板参数也是如此。

例如:

#include <vector>

std::vector<int, MyAllocator> v;

int complex_function(int long_param_name,
                     int& another_param_name);

int main(int argc, char** argv)
{
    if (argc == 2) {
        v.push_back(23);
    }
}

也就是说,cppreference 是一个 wiki,其他格式可能会漏掉。

【讨论】:

  • 虽然 MoS 存在,但没有人积极执行它,而“cppreference 编码风格”实际上是最活跃的编辑器的风格
【解决方案2】:

cppreference 没有严格的代码风格。即使在引用的页面上,NotesExample 中使用的函数也有两种不同的样式。如果您点击该页面上的链接,您还会发现用于结构的不同代码样式。

在引用的页面上:

int main() {
  X x;
  X x2 = X { x }; // copy-constructor (not aggregate initialization)
  Q q;
  Q q2 = Q { q }; // initializer-list constructor (not copy constructor)
}

int main()
{
    int n0{};     // value-initialization (to zero)
    int n1{1};

在该页面的第二个链接上:

struct A
{
    A() { }         // converting constructor (since C++11)  
    A(int) { }      // converting constructor
    A(int, int) { } // converting constructor (since C++11)
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 2021-09-18
    相关资源
    最近更新 更多