473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 2867
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
1
by: Lewis Baker | last post by:
I am having some template problems in VC7.1 The following code should compile fine but I get the error: 'warning C4667: 'void F(Traits<T>::P)' : no function template defined that matches...
2
by: Michael Stembera | last post by:
Here is a very simple piece of code to repro this bug. template<typename T, int N> inline bool foo( void ) { return true; } template<typename T> inline bool foo<T, 1>( void ) { return...
1
by: Michael Stembera | last post by:
I have a case of a very simple template class w/ a template method. If I define a specialization of the method outside the body of the template class it does not compile. Here is a tiny example...
4
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
5
by: Jeff Newman | last post by:
Hi all, I am trying to figure out what is causing the compile error in the following example. I have two functions which are identical (except one is templated), both trying to call a template...
0
by: James Kanze | last post by:
On May 16, 9:34 am, Paavo Helde <nob...@ebi.eewrote:
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
4
by: danilo.turina | last post by:
Hi all, today I encountered a problem that I'm only able to solve by using reinterpret_cast (and I'd like to avoid it). This was my code until yesterday (omitting includes, "using namespace"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.