我在cppreference上看到了很多示例代码。例如,下面的URL有一个代码。
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在哪里描述了编码风格?
发布于 2020-03-20 16:18:49
cppreference上的样式描述为@ Help:Manual of style (Code formatting)
对于间距和缩进,使用K&R variant。
如果函数的参数跨多行,则所有参数的缩进都与左括号匹配。模板参数也是如此。例如:
#include std::vector 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,其他格式可能会漏掉。
发布于 2020-03-20 16:17:08
cppreference上没有严格的代码样式。即使在引用的页面上,、Notes、和示例中使用的函数也有两种不同的样式。如果您点击该页面上的链接,您还会发现用于结构的不同代码样式。
在引用的页面上:
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)
};https://stackoverflow.com/questions/60770492
复制相似问题