C++ 命名约定
C++ Naming Conventions
我想知道 C++ 中的命名约定,特别是大写的位置和内容。我想我要问的是(来自 Java 和 C# 等语言)为什么 std::vector 和 std::string 没有大写?
作为后续,如果我将 std::string 类型定义为其他东西(在我的例子中是 "world"),我应该将其命名为 ...
typedef std::string world;
或
typedef std::string World;
我查看了以前的帖子和风格指南(下面列出了几个),但 none 似乎直接解决了这个问题(下面的链接)。 Google 指南说:
"The names of all types — classes, structs, typedefs, and enums — have the same naming convention. Type names should start with a capital letter and have a capital letter for each new word. No underscores." 所以这看起来应该是后者——但这看起来很奇怪,因为 std 库甚至没有稍微遵循这个约定。这只是一群人在样式标准化之前编写 std 库而现在(出于兼容性原因)change/update 为时已晚的事情之一吗?
谢谢!
之前的研究:
没有标准化的风格。保持一致。
但是,用户定义的类型通常以大写字母开头,这样它们就不会与内置类型或标准库中的类型混淆。
总之应该是:
typedef std::string World;
或者在 C++11 中你可以这样写:
using World = std::string;
我觉得 link 类似的问题会对您有所帮助:
What is the preferred naming conventions du jour for c++?
就像其他大多数人提到的那样,做两件事很重要:
- 选择一种风格
- 坚持下去
我想知道 C++ 中的命名约定,特别是大写的位置和内容。我想我要问的是(来自 Java 和 C# 等语言)为什么 std::vector 和 std::string 没有大写?
作为后续,如果我将 std::string 类型定义为其他东西(在我的例子中是 "world"),我应该将其命名为 ...
typedef std::string world;
或
typedef std::string World;
我查看了以前的帖子和风格指南(下面列出了几个),但 none 似乎直接解决了这个问题(下面的链接)。 Google 指南说: "The names of all types — classes, structs, typedefs, and enums — have the same naming convention. Type names should start with a capital letter and have a capital letter for each new word. No underscores." 所以这看起来应该是后者——但这看起来很奇怪,因为 std 库甚至没有稍微遵循这个约定。这只是一群人在样式标准化之前编写 std 库而现在(出于兼容性原因)change/update 为时已晚的事情之一吗?
谢谢!
之前的研究:
没有标准化的风格。保持一致。
但是,用户定义的类型通常以大写字母开头,这样它们就不会与内置类型或标准库中的类型混淆。
总之应该是:
typedef std::string World;
或者在 C++11 中你可以这样写:
using World = std::string;
我觉得 link 类似的问题会对您有所帮助:
What is the preferred naming conventions du jour for c++?
就像其他大多数人提到的那样,做两件事很重要:
- 选择一种风格
- 坚持下去