M
ma740988
I was perusing a thread a while back where the OP was trying to
determine how to express global data without polluting every
translation unit (TU) with global data a TU may not care about. There
was discussion about the use of a 'template constant trick' (I suspect
there's another name for this because I've come up short during a
seach of my texts and google) or a myers singleton. So now
consider:
# include <limits>
# include <iostream>
///form a
template < typename dummy >
struct consts // constants
{
//static int const MAX_INT = std::numeric_limits < int >::max
() ; //Visual studio 9 complains... check standard, i think this is
legal
static int const MAX_INT = INT_MAX ;
static double const PI ;
};
typedef consts<void> constants;
double const constants:I = 3.14159;
///form b
class c_constants {
public :
double const PI ;
static int const MAX_INT = INT_MAX ;
c_constants()
: PI ( 3.14159 )
{}
static c_constants& instance() {
static c_constants inst ;
return inst ;
}
};
int main()
{
std::cout << constants:I << '\n';
std::cout << c_constants::instance().PI << '\n';
std::cin.get();
}
It seems to me that the prime difference between the two foms (a) and
(b) is that with (a) I'm unable to express PI without the static
keyword. True/False?
One other thing: There was discussion about static initialization
fiasco so consider:
class foo {
static int const MAGIC = 0xBEEF ;
public :
static foo& instance () { static foo inst; return inst; }
void do_work()
{ int const whatever = c_constants::instance().MAX_INT -
MAGIC; }
} ;
Here a foo object could potentially be created/initialized before
c_constant object which is used in the do_work method. Would the
static initialization fiasco matter here?
determine how to express global data without polluting every
translation unit (TU) with global data a TU may not care about. There
was discussion about the use of a 'template constant trick' (I suspect
there's another name for this because I've come up short during a
seach of my texts and google) or a myers singleton. So now
consider:
# include <limits>
# include <iostream>
///form a
template < typename dummy >
struct consts // constants
{
//static int const MAX_INT = std::numeric_limits < int >::max
() ; //Visual studio 9 complains... check standard, i think this is
legal
static int const MAX_INT = INT_MAX ;
static double const PI ;
};
typedef consts<void> constants;
double const constants:I = 3.14159;
///form b
class c_constants {
public :
double const PI ;
static int const MAX_INT = INT_MAX ;
c_constants()
: PI ( 3.14159 )
{}
static c_constants& instance() {
static c_constants inst ;
return inst ;
}
};
int main()
{
std::cout << constants:I << '\n';
std::cout << c_constants::instance().PI << '\n';
std::cin.get();
}
It seems to me that the prime difference between the two foms (a) and
(b) is that with (a) I'm unable to express PI without the static
keyword. True/False?
One other thing: There was discussion about static initialization
fiasco so consider:
class foo {
static int const MAGIC = 0xBEEF ;
public :
static foo& instance () { static foo inst; return inst; }
void do_work()
{ int const whatever = c_constants::instance().MAX_INT -
MAGIC; }
} ;
Here a foo object could potentially be created/initialized before
c_constant object which is used in the do_work method. Would the
static initialization fiasco matter here?