J
Jason S
is there any way to use templates to bind integer/floating point
constants to a template for compile-time use?
e.g.
template <double conversion>
class meters
{
const factor = conversion;
double x;
public:
double raw() { return x; }
double value() { return x*conversion; }
...
};
typedef meters<39.4> inches;
double foo = inches::factor;
I've seen lots of places where you see template type arguments stored
for later use, e.g.
template <class T>
class foo
{
typedef T rawtype;
};
foo::rawtype blah;
My reason for doing this, is that I will have a lot of objects that are
"small" (their member variables consisting of exactly one integer or
floating-point number) in various groups with the same conversion
factor in each group; there may be lots of assignments between objects
& it makes no sense for a constant number to have to be copied around
all over the place. (in my case this is for an embedded system app; the
memory hit isn't a big problem, but the extra execution time to make
unnecessaray copies of constants would be bad.)
constants to a template for compile-time use?
e.g.
template <double conversion>
class meters
{
const factor = conversion;
double x;
public:
double raw() { return x; }
double value() { return x*conversion; }
...
};
typedef meters<39.4> inches;
double foo = inches::factor;
I've seen lots of places where you see template type arguments stored
for later use, e.g.
template <class T>
class foo
{
typedef T rawtype;
};
foo::rawtype blah;
My reason for doing this, is that I will have a lot of objects that are
"small" (their member variables consisting of exactly one integer or
floating-point number) in various groups with the same conversion
factor in each group; there may be lots of assignments between objects
& it makes no sense for a constant number to have to be copied around
all over the place. (in my case this is for an embedded system app; the
memory hit isn't a big problem, but the extra execution time to make
unnecessaray copies of constants would be bad.)