By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,281 Members | 2,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,281 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.