我在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
编码风格描述在哪里?
cppreference上的样式描述于@Help:样式(Code formatting
(手册
对于间距和缩进,K&使用R变体。
如果函数的参数跨越几行,则所有参数的缩进都与左括号匹配。模板参数也是如此。
例如:
#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,其他格式可能会漏洞百出。
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)
};