我在cppreference上看到了许多示例代码。 例如,以下网址只有一个代码。
https://en.cppreference.com/w/cpp/language/list_initialization
从上面的示例中,我们可以看到在struct
和function
上括号的缩进如下所示。
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
哪里描述了编码风格?
答案 0 :(得分:3)
@ Help:Manual of style(Code formatting
)中描述了cppreference的样式
对于间距和缩进,使用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,其他格式可能会漏掉裂缝。
答案 1 :(得分:1)
cppreference上没有严格的代码样式。即使在参考页面上,注释和示例中使用的函数也有两种不同的样式。如果您点击该页面上的链接,您还将找到用于结构的不同代码样式。
在引用的页面上:
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)
};