Using an underscore in a C++ identifier is simple, but improper use can cause naming conflicts, unexpected errors, and even undefined behavior. Knowing how to use leading underscores, trailing underscores, and snake_case is essential for writing safe, functional, and abstract C++ code. Read this article to discover the best practices that every C++ developer should know.
Table of Contents:
What is an Identifier?
In C++, an identifier is the name used for variables, functions, classes, etc. The identifier must start with a letter (A-Z, a-z) or an underscore(_), digits (0-9), or more underscores. Incorrect usage of underscores can lead to undefined behavior or naming conflicts.
Use Cases of Underscore in Identifiers
- Used for internal/private variables or functions.
- Prevent the conflicts with common names.
- Increases the readability.
- For specifying the name styles, some frameworks and libraries use underscores.
Rules for Using Underscores in C++ Identifiers
1. Leading Underscore(_identifier)
User-defined names are allowed but not recommended. For implementation purposes in the system, compiler, or standard libraries, the identifiers must be used in global scope.
Example:\
Here, the variable _myVar is allowed, in the global space, the usage of underscore at the beginning of a name is not recommended. This may conflict with the system, compiler, or standard library, which are reserved for internal use.
2. Trailing Underscore(identifier_)
The usage of underscore at the end of the name is safe, it’s a common convention to use them for private member variables in classes. To keep the code readable, the underscores help to avoid the problems with C++ keywords.
Example:
int value_ is used for coding conventions to indicate the member variables, it avoids the conflicts with the class keywords in C++.
3. Double Leading Underscore(__identifier)
Names starting with an underscore containing a double underscore are reserved for the compiler or system use. However, avoid them for variables, functions, or classes because they may lead to conflicts.
Example:
Using such names may lead to unpredictable behavior because the compiler or standard library may already be using them.
4. Leading Underscore Followed by an Uppercase Letter(_X)
Names beginning with a single underscore followed by capital letters are reserved for the standard library or system implementation. But don’t use them for variables, functions, or classes, as that can lead to conflicts.
Example:
Using such names may lead to unpredictable behavior because the standard library may already be using them.
5. Single Underscore in Middle(some_var)
As an additional point, this is a conventional rule followed in snake_case naming conventions, where words in a naming convention are separated with an underscore to increase readability. This rule applies to almost all function names, variable names, class names, and file names in most programming languages.
Example:
C++ Program: Proper Use of Underscore
C++ program that demonstrates the correct usage of Underscore
Output:
The program underscores are well placed (those are the right ones to use). The trailing underscores are used to indicate that the employee class private member variables id_ and name_ are private class member variables whose name will be different than the function arguments. main() function local variables (like user_input and user_name) follow Single Underscore in Middle case for better readability
Best Practices for Using an Underscore in an Identifier
- Using trailing underscores trailing _ for class members to distinguish from parameters.
- In the global and main scope, do not use a leading underscore.
- Avoid double-leading underscores for name assignments
Conclusion
Using underscores properly in C++ improves code readability and prevents naming conflicts. However, this is where a leading underscore helps in separating class members, and a single underscore in the middle makes naming things a little clearer. Do not use a leading underscore or double leading underscore; those are reserved. When you follow these rules, the code is clean and maintainable.
FAQs
1. What is an identifier in C++?
Identifiers are the names we have for variables, functions, classes, etc., and must start with a letter or an underscore.
2. Should we worry about leading underscores in C++?
It is allowed but not recommended in the Global scope, system-reserved names may conflict with it.
3. Why do we use a trailing underscore (variable_) in C++?
The trailing underscore helps to distinguish between class member variables, constructor, and function parameters.
4. Can I use double leading underscores (__var)?
No, names starting with double leading underscores are reserved for the compiler and standard library.
5. How to use underscore in C++ identifiers properly?
Use trailing underscores for privates, snake_case for readability, and no leading underscores unless in a global scope.