template arguments / compile-time binding of int/float/double numbers

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.)
 
M

Markus Schoder

Jason said:
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>

Not legal C++
class meters
{
const factor = conversion;

You probably meant

static const double factor = conversion;

Also not legal C++.
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.)

You could do something like

template <typename conversion>
class meters : public conversion
{
double x;

public:
double raw() { return x; }
double value() { return x*conversion::factor(); }

...

};

class meters2inches
{
public:
static double factor() {return 39.4;}
};

typedef meters<meters2inches> inches;
double foo = inches::factor();
 
J

Jason S

Markus said:
You could do something like

template <typename conversion>
class meters : public conversion
{
double x;

public:
double raw() { return x; }
double value() { return x*conversion::factor(); }

...

};

class meters2inches
{
public:
static double factor() {return 39.4;}
};

typedef meters<meters2inches> inches;
double foo = inches::factor();

thanks. hope the compiler in question is smart enough to do the
appropriate optimizations at compile-time.
 
M

mlimber

Jason said:
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.)

Well, chances are you are fretting prematurely and unnecessarily about
optimization (see, e.g., "Beware Premature Optimization" in
http://www.gotw.ca/publications/mill09.htm). You can, however, do this
with ints:

template<typename T, unsigned int N>
struct Array
{
T data[ N ];
};

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top