04 July 2022

C++ 核心指南目录

理由

这个是语言规则,把 main 声明成 void 可能会跟某些系统不兼容。

// -*- compile-command: "g++ -std=c++20 code.cpp && ./a"; -*-
void main() {/* ... */ };  // bad, not C++
error: '::main' must return 'int'

返回值类型改成 int ,编译器就不会报错了。

// -*- compile-command: "g++ -std=c++20 code.cpp && ./a"; -*-
#include <iostream>
int main()
{
    std::cout << "This is the way to do it\n";
}
This is the way to do it

注意

我们提这个规则,是因为总是有人坚持以错误的方式写代码。

强化

  • 编译器会报错
  • 如果编译器不报错,让代码检查工具标记错误