template metaprogramming

M

Mark

is there a way to make so i dont have to call the following with the
extra ::val portion?

template<int r, int g, int b>
struct RGB {
static const int val = 65536 * r + 256 * g + b;
};

ie RGB<50,100,150> instead of RGB<50,100,150>::val
 
V

Victor Bazarov

Mark said:
is there a way to make so i dont have to call the following with the
extra ::val portion?

template<int r, int g, int b>
struct RGB {
static const int val = 65536 * r + 256 * g + b;
};

ie RGB<50,100,150> instead of RGB<50,100,150>::val


You don't "call" it. You probably use it in an expression. If it is
an expression that expects an int, you can do

template<int r, int g, int b>
struct RGB {
operator int() const { return 65536 * r + 256 * g + b; }
};

and use it like

int a = RGB<50,100,150>();

V
 
L

lancediduck

templates do not have to evaluate expressions until they are actually
instantiated. So just decarling typedef RGB<100,1,1> my_fav_color;
does not assign a value to val. When val is actually referenced, then
it is evaluated.
In any case, how would you use the calculated value without accessing
"val"???
 
M

Mark

Victor Bazarov,

well. i meant use in an expression... it's similar to a function in
some sense, even if it doesnt operate the same way (ie, it's
precomputed).

anyways RGB<50,100,150>() is no better than RGB<50,100,150>::val ...
i'm just trying to be lazy, and it looks ugly :\

(e-mail address removed),

how would you use the calculated value without accessing
"val"???

well.. i suppose that's what im asking.

RGB<50,100,150> is only supposed to "return" a single value. so there
is no ambiguity between it returning ::val or ::coconut.

i'm wondering if there is another way to write the template so that it
automatically produces this value... without having to assign it to
some dummy variable first such as val.

and what exactly would
typedef RGB<100,1,1> my_fav_color
do??? would you have to use it like my_fav_color::val or what?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

is there a way to make so i dont have to call the following with the
extra ::val portion?

template<int r, int g, int b>
struct RGB {
static const int val = 65536 * r + 256 * g + b;
};

ie RGB<50,100,150> instead of RGB<50,100,150>::val

Wouldn't it be better as a function?

inline int RGB(int r, int g, int b)
{
return 65536 * r + 256 * g + b;
}

And then when using it you replace the < and > with ( and ) and it's all
you wanted, ie. RGB(50, 100, 150).

Erik Wikström
 
J

John Harrison

Erik said:
Wouldn't it be better as a function?

inline int RGB(int r, int g, int b)
{
return 65536 * r + 256 * g + b;
}

And then when using it you replace the < and > with ( and ) and it's all
you wanted, ie. RGB(50, 100, 150).

Erik Wikström

As a function it's not a compile time constant. Why the OP would want an
RGB value as a compile time constant I'm not sure but maybe there's a
reason.

Of course the other option is a macro

#define RGB(r, g, b) (65536*(r) + 256*(g) + (b))

Now it's a compile time constant and you have the convenient syntax.

john
 
M

Mark

Erik,
as John said, it's not compile time constant.

John,
that's not compile time either. that just replaces any occurance of
RGB(r, g, b) with (65536*(r) + 256*(g) + (b)), it still has to do the
calculations in run time.

Why do I want it during compile time? Because I'm never going to use
any variables, only constants...

Oh well. Just wondering if there was a better way. Thanks guys.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Erik,
as John said, it's not compile time constant.

John,
that's not compile time either. that just replaces any occurance of
RGB(r, g, b) with (65536*(r) + 256*(g) + (b)), it still has to do the
calculations in run time.

Why do I want it during compile time? Because I'm never going to use
any variables, only constants...

Oh well. Just wondering if there was a better way. Thanks guys.

If you are not going to use _any_ variables then you should be able to
hardcode the value directly.

Erik Wikström
 
J

John Harrison

Mark said:
Erik,
as John said, it's not compile time constant.

John,
that's not compile time either. that just replaces any occurance of
RGB(r, g, b) with (65536*(r) + 256*(g) + (b)), it still has to do the
calculations in run time.

Not if you are using constants for the values of r, g and b. RGB(0, 0,
0) is a compile time constant.

Why do I want it during compile time? Because I'm never going to use
any variables, only constants...

So RGB will give a compile time constant.
Oh well. Just wondering if there was a better way. Thanks guys.

Perhaps you need to quote the code you say doesn't compile. You must be
doing something wrong.

john
 
M

Mark

Erik said:
If you are not going to use _any_ variables then you should be able to
hardcode the value directly.

Well. I could. But I'd have to do it for many values... not a huge
deal, was just curious.


John said:
Not if you are using constants for the values of r, g and b. RGB(0, 0,
0) is a compile time constant.

Hm.. so it doesn't actually perform any calculations during run
time...?
It won't attempt to do (65536*(0) + 256*(0) + (0)) when the program is
run, but actually pre-determine the answer is 0?

That's interesting. Wish there was a way to test that out...

But yes, I shall use this then.


So, in theory, I could do

#define RGB(r, g, b) (65536*(r) + 256*(g) + (b))

and then do

#define WHITE RGB(255,255,255)

and it will replace all occurences of "WHITE" with 16777215?

Perhaps you need to quote the code you say doesn't compile. You must be
doing something wrong.

Hm? Did I say something doesn't compile?
 
J

John Harrison

Mark said:
Well. I could. But I'd have to do it for many values... not a huge
deal, was just curious.





Hm.. so it doesn't actually perform any calculations during run
time...?
It won't attempt to do (65536*(0) + 256*(0) + (0)) when the program is
run, but actually pre-determine the answer is 0?

That's interesting. Wish there was a way to test that out...

Easy, array bounds in C++ must be compile time constants (at least in a
standard conforming compiler). So try this

char x[RGB(0, 0, 1)];

When that compiles (which it will) it proves that RGB with constant
values produces compile time constants.
But yes, I shall use this then.


So, in theory, I could do

#define RGB(r, g, b) (65536*(r) + 256*(g) + (b))

and then do

#define WHITE RGB(255,255,255)

and it will replace all occurences of "WHITE" with 16777215?
Correct.



Hm? Did I say something doesn't compile?

You were so confident that you were right, I thought you must have tried
to compile something.

john
 
P

peter koch

John Harrison skrev:
Not if you are using constants for the values of r, g and b. RGB(0, 0,
0) is a compile time constant.

Just wanted to add that it does not matter if you use an inline
function or a macro here - unless you need a compile time constant. On
all real-world compilers the inline function will be replaced by a
constant value (no run-time overhead).

/Peter
 
M

Mark

oh. well then. i guess i learned something about my compiler today :)
very nifty.

You were so confident that you were right, I thought you must have tried
to compile something.

oh. i see what you were getting at now. didn't realize i just shot your
point down without looking into it. sorry John :) you were right.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top