What are the rules about using an underscore in a C++ identifier?

Introduction

In the world of C++, naming conventions play a crucial role in writing clean and maintainable code. One common naming convention is to use an underscore (_) as a prefix or suffix in C++ identifiers to denote certain characteristics of variables.

Using an Underscore in C++ Identifiers

In C++, using an underscore in an identifier is allowed and is just a matter of coding style. However, there are certain rules and recommendations regarding the usage of an underscore in C++ identifiers.

1. Leading Underscore

According to the C++ standard, it is recommended to avoid using an underscore as a leading character followed by an uppercase letter (e.g., _Variable) as it is reserved for language implementation use. This means that identifiers starting with an underscore and followed by a capital letter are not portable and may cause issues when compiled with different compilers.


// Incorrect usage of leading underscore
int _Variable = 10;
        

Instead, it is recommended to use a lowercase character or a capital letter as the first character of an identifier.


// Correct usage without a leading underscore
int variable = 10;
        

2. Trailing Underscore

Using a trailing underscore in an identifier (e.g., Variable_) is allowed by the C++ standard and does not cause any issues with portability. It is often used to distinguish between a variable and a function or to avoid conflicts with reserved keywords.


// Usage of trailing underscore
int class_ = 5;

void function_() {
    // ...
}
        

By using a trailing underscore, it becomes easier to differentiate between variables and function names and avoid naming conflicts.

3. Double Underscore

A double underscore (__) at the beginning or end of an identifier is reserved for use by the compiler and should be avoided in user code to prevent conflicts with the implementation.


// Incorrect usage of double underscore
int __Variable__ = 10;
        

To ensure portability and avoid potential issues, it is best to follow the guidelines provided by the C++ standard and avoid using double underscores in identifiers.

4. Underscore in Namespaces, Classes, and Non-member Functions

When using an underscore in a namespace, class, or non-member function name, there are specific rules to follow. According to the C++ standard, an underscore should not be used as a leading character in these cases:


// Incorrect usage of underscore in a namespace
namespace _Namespace {
    // ...
}

// Incorrect usage of underscore in a class name
class _Class {
    // ...
}

// Incorrect usage of underscore in a non-member function name
void _function() {
    // ...
}
        

Instead, it is recommended to use a lowercase character or a capital letter as the first character:


// Correct usage without a leading underscore in a namespace
namespace Namespace {
    // ...
}

// Correct usage without a leading underscore in a class name
class Class {
    // ...
}

// Correct usage without a leading underscore in a non-member function name
void function() {
    // ...
}
        

Conclusion

In C++, using an underscore in identifiers is allowed, but there are certain rules and recommendations to follow. It is advisable to avoid using a leading underscore followed by an uppercase letter, as it is reserved for implementation use and can cause portability issues. Trailing underscores are commonly used to differentiate variables from functions or to avoid conflicts with reserved keywords. Double underscores should be avoided to prevent conflicts with compiler implementation. By following these guidelines, you can write clean and portable C++ code.