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

๐ Hey there, fellow tech enthusiasts! ๐ฉโ๐ป๐จโ๐ป
In today's blog post, we're going to address an interesting and commonly asked question in the C++ programming language: what are the rules about using an underscore in a C++ identifier? ๐๐ป
๐ค So, let's dive right in, shall we?
When it comes to naming member variables in C++, it's not uncommon to use some sort of prefix to differentiate them from local variables or parameters. For folks familiar with the MFC framework, the convention of using "m_" as a prefix (e.g., "m_foo") is quite popular. On the other hand, some developers prefer using something like "myFoo" to indicate a member variable.
But, here's where it gets interesting! If you come from a C# (or possibly just .NET) background, you might have seen the convention of using just an underscore for member variable names, like "_foo". The question is, does this convention hold up in C++ according to the standard? ๐ค๐
๐ The C++ Standard
According to the C++ standard, using an underscore as the first character of an identifier is allowed. However, there are a few rules we need to keep in mind:
1๏ธโฃ Rule #1: Names that begin with an underscore followed by an uppercase letter are reserved for use by the implementation. So, avoid using identifiers like "_Foo" or any variations thereof, starting with an underscore followed by a capital letter.
2๏ธโฃ Rule #2: Names that begin with an underscore are also reserved at global or namespace scope. This means it's best to avoid using identifiers like "_bar" for global variables or namespace-level entities.
3๏ธโฃ Rule #3: Names that begin with an underscore followed by another underscore anywhere after the first character are also reserved. So, steer clear of identifiers like "__baz".
โจ Easy Solutions and Best Practices
Now that we know the rules, you might be wondering how to adopt a naming convention that works well in C++. Here are a couple of easy solutions and best practices to guide you:
1๏ธโฃ Solution #1: Use a consistent prefix convention, such as "m_" or "my" (e.g., "m_foo" or "myFoo"). This ensures clarity and avoids any possible conflicts with reserved names mentioned earlier.
2๏ธโฃ Solution #2: If you find the C# convention appealing, you can use an underscore as a prefix for your member variables (e.g., "_foo"). However, it's crucial to adhere to the rules stated in the standard and avoid any potential conflicts.
๐ Calling All Code Warriors!
We've now demystified the rules and provided you with practical solutions for using underscores in C++ identifiers. ๐กโจ
Do you have any additional tips or tricks for naming conventions in C++? Share them with us in the comments section below! Let's create a vibrant and engaging discussion on this topic. ๐๐ฌ
Remember, in the ever-evolving world of programming, it's crucial to stay informed and be aware of language specifications and best practices. Stay curious, keep learning, and happy coding! ๐๐ป
P.S. Don't forget to subscribe to our newsletter for more exciting tech tips, tricks, and tutorials! ๐๐
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.


