Global variables, what are they exactly? - c++

First of all, I'm new to c++, and 'trying' to prefix my variables.
But it isn't very clear to me.
So my question is, is it correct to prefix static variables with "g_"?
Thank you!
using namespace std;
// The main window class name.
static TCHAR g_szWindowClass[] = _T("win32app");
// The string that appears in the application's title bar.
static TCHAR g_szTitle[] = _T("Win32 App");
...

It's better to use a prefix than nothing that distinguishes global variables as such. But
it's even better to avoid global variables to the degree possible, and
instead of a C style prefix, in C++ you can use a named namespace.
It also has many advantages to avoid Microsoft's T macro silliness. It's in support of Windows 9x, and you're probably not targeting Windows 9x. Also, it has many advantages, not the least for maintenance, to avoid Microsoft's silly Hungarian notation thing, that is, prefixes like sz, which was in support of Microsoft's 1980's Programmers Workbench help system, which just like Windows 98 is not very relevant any longer.
Also, it can be advantageous to use const wherever practically possible.
Note that const at namespace level implies static storage class, so an explicit static is then no longer necessary.
Thus, instead of the current
// The main window class name.
static TCHAR g_szWindowClass[] = _T("win32app");
do
namespace g {
auto const windowClassName = L"win32app";
}
with
C++ namespace g instead of C prefix g_,
const added, guaranteeing that this variable is not modified, and
direct use of wide character literal instead of Microsoft Windows 9x T macros.
Then you can refer to g::windowClassName, or without the prefix after a using namespace g;, or even with an alias for g.
The particular braces convention I use for namespaces is in support of nested namespaces without the indentation hassle. Unfortunately that's not supported by common editors.

C++ has no official naming conventions. It does have a few rules for variable names, or identifers in general, which you have to follow, but other than that, names are entirely up to you, with all the flexibility and dangers it brings (much like the rest of the language).
Here is a good overview of the rules: http://en.cppreference.com/w/cpp/keyword
So, for example, _G_szTitle would be wrong, but g_szTitle is OK.
The real problem is that you almost certainly do not want to use globals. Global variables are almost always bad design. Avoid them.
Another, smaller, problem is that you use the so-called "Hungarian notation". Google a bit for it to find out why many people (myself included) are opposed to it, especially in a language like C++.

The most obvious definition of a global variable is a variable declared at namespace scope (including the outermost namespace).
Now, you could argue that a variable declared at namespace scope which is also declared static and, thus, isn't visible outside the given translation unit. Likewise, a variable declared in an unnamed namespace might be considered non-global. However, both of these kinds of variables shared many of the the bad properties of global variables. For example, they introduce a serialization point when being accessed from multiple threads.
Thus, I consider actually a wider range of variables to be global, i.e., also static data members in classes and function locale static variables. Each of these also exists just once throughout a a program. Just because these constructs happen to be used for some [anti] design patterns (notable Singleton) doesn't magically bless global variables!
With respect to prefixing variables names: do not include type prefix into your variable names! In C++ types are already sufficiently checked by the compiler. Including the type tends to result in eventually incorrect names. Specifically with respect to global variables, here is my recommendation for their prefix: whenever you want to use the prefix for a global variable stop whatever you are doing! You are in the process of constructing a problem and you should rather seek to change the design to remove the need for the global variable!

C++11 Standard (draft n3337):
17.6.4.3.2 Global names [global.names]
Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore __ or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
Other than these there aren't any restrictions on the (identifier) names you choose for global variables.
It's a convention used by some to prefix global variables by g_, member variables by m_, etc. This is a matter of choice; the language itself doesn't impose such a requirement. So you're free to name them anything and prefix them with anything as long as the identifier starts with an English alphabet.
As for the usage of global variables, I would say if you are just beginning to learn C++, use them, get hurt and then realize how they are bad; you'll see why they are always condemned by experienced programmers. Just telling they're bad would add little value, some things are better learned by experience.

Related

Is every "normal" use of user-defined literals undefined behavior?

User defined literals must start with an underscore.
This is a more or less universally well-known rule that you can find on every layman-worded site talking about user literals. It is also a rule which I (and possibly others?) have been blatantly ignoring ever since on a "what a bullshit" base. Now of course, that's strictly not correct. In the strictest sense, this uses a reserved identifier, and thus invokes Undefined Behavior (although you don't get as much as a shrug from the compiler, practically).
So, pondering whether I should continue to deliberately ignore that (in my opinion useless) part of the standard or not, I decided to look at what's actually written. Because, you know, what does it matter what everybody knows. What matters is what's written in the standard.
[over.literal] states that "some" literal suffix identifiers are reserved, linking to [usrlit.suffix]. The latter states that all are reserved, except those that start with an underscore. OK, so that's pretty much exactly what we already knew, explicitly written (or rather, written backwards).
Also, [over.literal] contains a Note which hints to an obvious but troubling thing:
except for the constraints described above, they are ordinary namespace-scope functions and function templates
Well, sure they are. Nowhere does it say that they aren't, so what else would you expect them to be.
But wait a moment. [lex.name] explicitly states that each identifier that begins with an underscore in the global namespace is reserved.
Now, a literal operator usually, unless you explicitly put it into a namespace (which, I believe nobody does!?) is very much in the global namespace. So, the name, which must begin with an underscore, is reserved. There is no mention of a special exception. So, every name (with underscore, or without) is a reserved name.
Are you indeed expected to put user defined literals into a namespace because the "normal" usage (underscore or not) is using a reserved name?
Yes: the combination of forbidding the use of _ as the start of a global identifier coupled with requiring non-standard UDLs to start with _ means that you can't put them in the global namespace. But you shouldn't be dirtying up the global namespace with stuff, especially UDLs, so that shouldn't be much of a problem.
The traditional idiom, as used by the standard, is to put UDLs in a literals namespace (and if you have different sets of UDLs, then you put them in different inline namespaces below that namespace). That literals namespace is typically underneath your main one. When you want to use a particular set of UDLs, you invoke using namespace my_namespace::literals or whichever sub-namespace contains your literal set of choice.
This is important because UDLs tend to be heavily abbreviated. The standard for example uses s for std::string, but also for std::chrono::duration of seconds. While they do apply to different kinds of literals (s applied to a string is a string, while s applied to a number is a duration), it can sometimes be confusing to read code that uses abbreviated literals. So you shouldn't throw literals at all users of your library; they should opt-in to using them.
By using different namespaces for these (std::literals::string_literals and std::literals::chrono_literals), the user can be up-front about which sets of literals they want in which parts of code.
This is a good question, and I'm not sure about the answer, but I think the answer is "no, it's not UB" based on a particular reading of the standard.
[lex.name]/3.2 reads:
Each identifier that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
Now, clearly, the restriction "as a name in the global namespace" should be read as applying to the entire rule, not just to how the implementation may use the name. That is, its meaning is not
"each identifier that begins with an underscore is reserved to the implementation, AND the implementation may use such identifiers as names in the global namespace"
but rather,
"the use of any identifier that begins with an underscore as a name in the global namespace is reserved to the implementation".
(If we believed the first interpretation, then it would mean that no one could declare a function called my_namespace::_foo, for example.)
Under the second interpretation, something like a global declaration of operator""_foo (in the global scope) is legal, because such a declaration does not use _foo as a name. Rather, the identifier is just a part of the actual name, which is operator""_foo (which does not start with an underscore).
Is every “normal” use of user-defined literals undefined behavior?
Clearly not.
The following is the idiomatic (and thus definitely “normal”) use of UDLs, and it’s well-defined according to the rule you’ve just listed:
namespace si {
struct metre { … };
constexpr metre operator ""_m(long double value) { return metre{value}; }
}
You’ve listed problematic cases and I agree with your assessment about their validity but they’re easily avoided in idiomatic C++ code so I don’t entirely see the problem with the current wording, even if it was potentially accidental.
According to the example in [over.literal]/8, we can even use capital letters after the underscore:
float operator ""E(const char*); // error: reserved literal suffix (20.5.4.3.5, 5.13.8)
double operator""_Bq(long double); // OK: does not use the reserved identifier _Bq (5.10)
double operator"" _Bq(long double); // uses the reserved identifier _Bq (5.10)
The only problematic thing thus seems to be the fact that the standard makes the whitespace between "" and the UDL name significant.
Yes, defining your own user defined literal in the global namespace results in an ill-formed program.
I haven't run into this myself, because I try to follow the rule:
Don't put anything (besides main, namespaces, and extern "C" stuff for ABI stability) in the global namespace.
namespace Mine {
struct meter { double value; };
inline namespace literals {
meter operator ""_m( double v ) { return {v}; }
}
}
int main() {
using namespace Mine::literals;
std::cout << 15_m.value << "\n";
}
This also means you cannot use _CAPS as your literal name, even in a namespace.
Inline namespaces called literals is a great way to package up your user defined literal operators. They can be imported where you want to use it without having to name exactly which literals you want, or if you import the entire namespace you also get the literals.
This follows how the std library handles literals as well, so should be familiar to users of your code.
Given the literal with suffix _X, the grammar calls _X an "identifier".
So, yes: the standard has, presumably inadvertently, made it impossible to create a UDT at global scope, or UDTs that start with a capital letter, in a well-defined program. (Note that the former is not something you generally want to do anyway!)
This cannot be resolved editorially: the names of user-defined literals would have to have their own lexical "namespace" that prevented clashes with (for example) names of implementation-provided functions. In my opinion, though, it would have been nice for there to be a non-normative note somewhere, pointing out the consequences of these rules and pointing out that they are deliberate.

What does using namespaces offer the programmer over just prefixing the function name?

E.g. SFML::Render() vs SFML_Render()
I've noticed in libraries that offer C and C++ bindings, that often the C++ versions make use of namespaces (SFML, libtcod) and the C bindings do the same thing but just prefix the name with what library they belong to.
Both of them require the programmer to prefix the function, both give context as to where they belong, both of them perform the same function. I'm really confused as to what benefits namespaces offer over function prefixing.
using-declarations
You can write using SFML::Render after which you can simply call the function with Render(), without needing the SFML:: in the front. The using-declaration can also be scoped to a function or class. This is not possible with prefixed names.
using-directives
You can also bring a whole namespace to the current scope with using namespace. Everyone knows what using namespace std does. These can also be scoped.
Namespace aliases
If you have a symbol with a long qualified name e.g. mylib::sublib::foo::bar::x, you can write namespace baz = mylib::sublib::foo::bar and then refer to x with just baz::x. These are scope-specific as well.
Among the C-style prefixed names there's usually nothing that big that would need an alias, and if there were you could just use a macro.
Adding to & removing from a namespace
If you have a file full of functions that need to be placed under a namespace x you can simply add two lines to make it happen: namespace x { and }. Removing from a namespace is equally simple. With prefixed names you have to manually rename each function.
Argument-dependent lookup
You may be able to omit the namespace qualification on a function call if the function lives in the same namespace as some of its arguments. For example, if namespace baz contains both an enum E and a function F that takes it, you can write F(baz::E) instead of baz::F(baz::E). This can be handy if you follow the style that prefers namespaced free functions over methods.
So in conclusion, namespaces are more flexible and offer more possibilities than the prefixed naming style.
SFML_Render is easy to find with fgrep -w SFML_Render, whereas SFML::Render may appear in some source files as just Render if the SFML namespace is implicit.
If you program in C++, there are strong benefits in using the C++ constructs, but you better use a powerful environment such as Eclipse or Visual Studio to help you make sense of all the added complexities.
If you want interoperability with C, you should not use namespaces nor overloading.
they way i see it there are severel uses for namespaces.
some of them:
Name isolation: define a package (a set of classes, functions,
globals, type definitions, etc.) in a namespace to ensure that when
included, these names do not conflict with existing code.
Version control: maintain multiple versions of the code.
and offcourse : A using statement can be used to make names available
without the :: scope operator
Ideally, namespaces should
• Express a logical coherent set of features
• Prevent user access to unrelated features.
• Impose minimal notational burden on users.

Defining the complex constant "i" in C++ (#define vs const)

I'm wondering what is the "best practice" to define the complex constant "i" in C++.
I know that the "#define vs const in C++" question has been asked multiple times, and that the general answer is that it's best to use const.
However, I'm thinking that it makes sense to use #define instead of const to define mathematical constants (such as "i" or "pi"), because we don't think of them as variables, but "absolute constants" (in the accepted answer here, one can read: "A constant defined with the const qualifier is best thought of as an unmodifiable variable."). Also, I see that in the math.h library, constants are defined this way, e.g. #define M_E 2.71828182845904523536028747135266250 /* e */.
So I'm wondering, how do C++ programmers usually define the complex constant i?
Lastly, I have a small issue with my current code #define I std::complex<double>(0.0, 1.0): precompilation causes a name clash with a Qt library that I use (as soon as I enable C++11 support).
Best practise is to declare a static const instance, with either a distinctive name or in a namespace.
Your #define does not define a mathematical constant. It defines a macro which expands to std::complex<double>(0.0, 1.0). Why are they different?
1. Scope
Every time the compiler finds a token called I, whether it could be a variable name or not, will be replaced. It doesn't matter if it's a type name, a template parameter, a variable or a function argument - it will be replaced. It doesn't matter if it's in a namespace either, since the preprocessor doesn't understand them. You've already seen this break Qt, which is precisely the reason macros are generally deprecated for declaring constants.
Where they are used, it's vital to make sure the name is unique - but there's no easy way to do this.
2. Semantics
If I declare a static constant variable (ie, one that doesn't vary despite the name), it's useable just like any instance of that type - and a smart optimizer can probably avoid loading the global. However, the macro declares a new anonymous temporary each time it is referenced. There will be at least some cases where the duplicate instances can't be elided.

When to use "::" for global scope in C++?

Every once in a while, I stumble across some code that I'm maintaining that challenges the way I think about my code style. Today was one of those days...
I'm aware that about why you would want to use the scope operator to define global scope. In fact, here scope resolution operator without a scope is a great link tell you why.
However, I saw something that made me think today. All the classes in question were wrapped into a namespace for the project (good!), but I did see copious usage of the global scope operator. Namely, it was used for everything from C libraries (with the exception of uint8_t and the like... yes, the programmer used the .h version of this library since apparently the version of g++ they were running still threw warnings about the new C++ standard). Is this useful? I view this as just as a waste of characters (reminds me of using the this pointer... except in the case of the copy constructor and assignment operator where it helps in clarifying which object is which). Am I missing something? Sure, someone can come around and name something along the lines of usleep() or stderr (where I saw the most usage of "::"), but won't they know that doing so will probably break something horribly? At what point do you say "screw it" in terms of the scope operator and just tell yourself that someone who names a function a certain way within your namespace is asking for trouble?
So my question is... what is the "correct" (subjective I understand) way of using the global scope operator in this context? Should everything not included in std or your own namespaces have the global scope explicitly defined? I tend to err on the side of caution and use "std::" to avoid the using directive, but is anything gained from using the global scope operator here? I tend to think for clarity's sake it does lend to the fact that we aren't getting the variable or function in question from the current namespace, but I'm torn between including it and not given today's developments.
As always, thanks for the help and guidance as I look to make my code cleaner, more readable, and (of course) significantly more awesome.
I use it quite infrequently; only when some ambiguity needs resolving for whatever reason. This is pretty subjective, though.
There may be some occasions (say, inside a template) where you're worried about ADL causing ambiguities only in certain cases:
template <typename T>
void foo(T t)
{
::bar(t); // :: just in case there's a `bar` in `T`'s namespace
}
There is almost no correct answer to this as it's almost totally style related, with the exception that if you think you may want to change from where you are going to import some declaration(s)/definition(s), in which case, when you use them, you don't specify any scope and import them using the using directive (to import a subset from the namespace) or the using namespace directive to import the entire set from the namespace.
Mostly the using directive is used as a convenience directive, but it is a powerful way to direct which declarations/definitions are used. My preference is to not specify the scope and import the declarations. Doing this allows for easy changes if ever they are needed while reducing visual noise. Also, specifying the scope would mean I'd be "locked in" from where I am getting the declarations (well, I'd have to do a global search and replace to change it).
If ever there is a conflict (you try an use a declared item with the same name that has been imported from more than one namespace) the compiler will let you know, so there's no real danger.
Readable code is that has the least amount of noise. namespace prefixes normally provide nothing but noise. So baseline is to not have them at all.
Namespaces were introduced to C++ mainly to handle 3rd party stuff out of one's control. To allow libraries drop prefixing, while clients can use terse names by applying using.
Just because you can have the same name in many namespaces does not imply it is a good idea to too. If a project uses some environment, platform API, library set, whatever that puts name in global, those names are better be avoided for other purposes. As with or without prefixes they will bear mental overhead.
Use of :: is rare in well-shaped code, and frequent uses appear in wrapper classes for that same functionality.
Consider the following cases.
Public library.
You are writing an exportable library with public headers. And you absolutely have no clue in what environment your headers will be included. For example, someone may do:
namespace std = my_space::std;
#include "your_header"
And all your definitions will be corrupted, if you simply use: std::list<int>, etc. So, it's a good practice to prepend :: to everything global. This way you can be absolutely sure what you're using. Of course, you can do using (in C++11) or typedef - but it's a wrong way to go in headers.
Collaborative .cc/.cpp files.
Inside your own code that is not exposed to public in any way, but still editable not only by you - it's a good practice to announce, what you're going to use from outside of your namespace. Say, your project allows to use a number of vectors - not only an std::vector. Then, where it's appropriate, you put a using directive:
// some includes
using vector = ::std::vector<int>; // C++11
typedef ::std::vector<int> vector; // C++03
namespace my_namespace {
...
} // namespace my_namespace
It may be put after all includes, or inside specific functions. It's not only gives control over the code, but also makes it readable and shortens expressions.
Another common case - is a mixing of global C functions and a C++ code. It's not a good idea to do any typedefs with function names. In this situation you should prepend C functions with the global scope resolution operator :: - to avoid compilation problems, when someone implements a function with a same signature in the same namespace.
Don't use :: for relative types and namespaces - or you'll lose the benefit of using namespaces at all.
Your own code.
Do what you want and how you want. There is only one good way - the way you fill comfortable with your own code. Really, don't bother about global scope resolution in this situation, if it's not requiered to resolve an ambiguity.

Control access to C++ global scope?

I sometimes have to transform some matured c source code into classes. A problem that sometimes arises is that some functions share global variables. This typically is hard to find.
I just was thinking about, if there is a possibility to disallow a class explicitly to use symbols from the global scope or anything like that. Any ideas?
EDIT:
Of course i could search for all global varialbles and transform them to class members, but that can be somewhat difficult. If the source code has some 1000 lines i can not review all the code. I just wonder, if the compiler could help me to find them.
You can put all the global variables in a namespace scope (may be more than 1). After this the compiler will complain for the ex-global variables. Just go and fix accordingly.
Edit: For the new question, No there is no facility from compiler which will single out the global variables. Moreover, finding global variables is easy and does not require code review or restructuring. It's a mechanical job. As soon as you find it, enclose it in a namespace scope. e.g.
int g_value;
converts to,
namespace Globals {
int g_value;
};