Is it possible to use "yes", "no", "i", "out" as a name for / enum variables?

I read the C ++ naming rule document, they seem to be useful names.

However, in practice, when I tried to create a variable / enumeration with a name like iter , yes , no , out , i , Error , etc., Visual Studio would strangely use an italic font for them.

enter image description here

I can only guess that they are reserved for special things, and an IDE (like the refactoring / renaming process) might act weird if I use such names.

Is it safe to use these names in practice? Am I just too panicky?

Sorry if this is too new or inappropriate question.
I doubt it for several weeks, but I'm too afraid to ask.

+5
source share
2 answers

These names are valid and will not cause any "harm", the standard says only:

  • Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter (2.11) is reserved for any use.

  • Each name starting with an underscore is reserved for implementation for use as a name in the global namespace.

This means that all your names can be used in custom code. Visual Studio may just have a thing for these names, since i and iter are commonly used in loops.

+4
source

These names are not reserved in standard C ++, as Rick Astley explained. An implementation may take additional reserved words to provide language extensions, such as a ref class in C ++ / CLI. In some cases, for example, with the ref class , where ref is the contextual keyword, these extensions only make malformed programs that are well formed in the scope of the extended language. In other cases, otherwise, a well-formed program may change its meaning or become ill-formed. In the first case, the implementation still complies with the C ++ standard if it provides all the required diagnostics; in the latter case, this is certainly not the case.

It is considered good practice to make the latter kind of extensions optional, for example. using the command line option, so the implementation still has a mode in which it is fully compliant. I immediately assume that VC ++ actually allows you to write well-formed programs containing yes , no , i , iter , which will behave in accordance with the requirements of the standard (despite implementation errors).

IDE is a different beast. It is considered outside the scope of the C ++ standard and may hinder or even hinder the writing of perfectly correctly formed code. It will still be a problem of quality of implementation or a problem of customer satisfaction if you do.

+1
source

Source: https://habr.com/ru/post/1269709/


All Articles