469,913 Members | 2,648 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

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

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

Jun 3 '06 #1
3 2686
Jason S wrote:
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();

Jun 3 '06 #2
Markus Schoder wrote:
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.

Jun 5 '06 #3
Jason S wrote:
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

Jun 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Pelle Beckman | last post: by
1 post views Thread by Lewis Baker | last post: by
5 posts views Thread by Jeff Newman | last post: by
4 posts views Thread by danilo.turina | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.