global constants - two alternatives...
Given:
Code:
# include <iostream>
//In a file called ... global_constants.h
class global_constants1
{
public :
static const int dummy = 5;
static const double dprec_value1 ;
};
//global_constants.h or global_constants.cpp
const double global_constants1::dprec_value1 = 4.3 ;
//In a file called ... global_constants.h
namespace global_constants2
{
const int dummy = 5;
double const dprec_value1 = 4.3 ;
}
int main()
{
std::cout << global_constants2::dummy << std::endl;
std::cout << global_constants1::dummy << std::endl;
std::cin.get();
}
The code show reflects two alternatives to global constants definition. I'm able to deduce from the standard that constants have internal linkage. That said, I think you'll get internal linkage in both versions and any decent compiler will eliminate unused symbols in translation unit that includes these files. Simply put there's no advantage of one over the other. True or False?
Re: global constants - two alternatives...
If you're really concerned about unused symbols and whatnot, then why not use the preprocessor? Simple constants are one area in which #defines don't have any real downside.
Re: global constants - two alternatives...
Quote:
Originally Posted by
Lindley
Simple constants are one area in which #defines don't have any real downside.
Unless you want to examine their value in the debugger.
Re: global constants - two alternatives...
Quote:
Originally Posted by
Lindley
If you're really concerned about unused symbols and whatnot, then why not use the preprocessor? Simple constants are one area in which #defines don't have any real downside.
My concern goes beyond 'unused symbols'. I'm trying to make sense of a coding standard that suggests using the 'global_constants1' option.
Re: global constants - two alternatives...
Quote:
Originally Posted by
VladimirF
Unless you want to examine their value in the debugger.
Possible, I suppose, if they're just aliased to other constants. In that case, another approach would probably be preferable. But for many cases, there's little incentive to directly examine them in the debugger since....well, they don't change from the value you assign them, by definition.
Re: global constants - two alternatives...
Quote:
Originally Posted by
Lindley
If you're really concerned about unused symbols and whatnot, then why not use the preprocessor? Simple constants are one area in which #defines don't have any real downside.
#define-s ignore namespaces. I try to avoid them as much as possible.
Re: global constants - two alternatives...
That's a good point, particularly when defining a library rather than an end program.