So I've been making a header file and put variables in their own namespace to avoid conflicts. My question is, do functions in the header file normally go in a namespace too, or should they just be named in a way which makes them unlikely to be accidentally copied?
I would say that they should be put in a namespace as well, for consistency.
For example, pretty much everything in the C++ standard library is in the std:: namespace.
Of course it matters that you choose a sensible (i.e. reasonably short) name for your namespace, and not something like MatsLibraryForCpp.
> be named in a way which makes them unlikely to be accidentally copied?
that's what namespaces do.
> Of course it matters that you choose a sensible (i.e. reasonably short) name for your namespace,
> and not something like MatsLibraryForCpp.
¿rationale?
Remember that you can do namespace a = overly_long::and::nested;
I hear the globals are bad mantra a lot, but in this case, I have about 15 variables, used by almost every function in the header, all placed in a namespace. Placing them in a namespace avoids naming conflicts, having them global avoid passing 15 variables around functions. I can hardly see there being performance issues with having these variables global, since they are so few. So what is the reasoning here for me passing all these variables from function to function for the entire header file?
> I have about 15 variables, used by almost every function in the header
Variables at namespace scope are not bad per se; it is tight coupling between components that we should strive to minimize.
1. For variables that do not form the logical interface of the component, but are required by many functions of the component:
a. Do not declare them in the header.
b. Define them with internal linkage in the implementation file.
2. For variables that form the logical interface of the component, but are deemed immutable in the iterface:
a. Declare const references to them in the header.
b. Define the actual mutable variables with internal linkage in the implementation file.
c. Define the const references with external linkage in the implementation file.
3. For variables that do form the logical interface of the component, do just you are doing; ie. what the standard library does with standard streams (std::cout and friends).
For cases 2. and 3., you would also need to ensure that these variables are initialized prior to their first use.
Similar considerations would apply to static member variables of classes; they are like variables at namespace scope.
I hear the globals are bad mantra a lot, but in this case, I have about 15 variables, used by almost every function in the header, all placed in a namespace. Placing them in a namespace avoids naming conflicts, having them global avoid passing 15 variables around functions.
It's difficult to say for sure without knowing what it is these variables and functions do... but it sounds like maybe this would be better written as a class.
If you don't mind, can you elaborate more on what these functions/variables are? This is mostly just for my own curiosity.
This was a very helpful thread. It's too far along to change (lazy me) how I did this particular header, but I think the next kind of thing like this I do, I now know what I'm doing and the entire design will be very different. Thanks! ^_^