 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mark Guest
|
Posted: Wed Nov 16, 2005 1:44 am Post subject: template metaprogramming |
|
|
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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Nov 16, 2005 1:56 am Post subject: Re: template metaprogramming |
|
|
Mark wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
lancediduck@nyc.rr.com Guest
|
Posted: Wed Nov 16, 2005 2:18 am Post subject: Re: template metaprogramming |
|
|
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"???
|
|
| Back to top |
|
 |
Mark Guest
|
Posted: Wed Nov 16, 2005 8:27 am Post subject: Re: template metaprogramming |
|
|
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 :
lancedid... (AT) nyc (DOT) rr.com,
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?
|
|
| Back to top |
|
 |
Erik Wikström Guest
|
Posted: Wed Nov 16, 2005 9:24 am Post subject: Re: template metaprogramming |
|
|
On 2005-11-16 02:44, Mark wrote:
| Quote: | 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
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Wed Nov 16, 2005 12:30 pm Post subject: Re: template metaprogramming |
|
|
Erik Wikström wrote:
| Quote: | On 2005-11-16 02:44, Mark wrote:
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
|
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
|
|
| Back to top |
|
 |
Mark Guest
|
Posted: Wed Nov 16, 2005 6:02 pm Post subject: Re: template metaprogramming |
|
|
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.
|
|
| Back to top |
|
 |
Erik Wikström Guest
|
Posted: Wed Nov 16, 2005 6:10 pm Post subject: Re: template metaprogramming |
|
|
On 2005-11-16 19:02, Mark wrote:
| Quote: | 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
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Wed Nov 16, 2005 6:17 pm Post subject: Re: template metaprogramming |
|
|
Mark wrote:
| Quote: | 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.
| Quote: |
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.
| Quote: |
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
|
|
| Back to top |
|
 |
Mark Guest
|
Posted: Wed Nov 16, 2005 11:46 pm Post subject: Re: template metaprogramming |
|
|
Erik Wikström wrote:
| Quote: | 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 Harrison wrote:
| Quote: | 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?
| Quote: | 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?
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Thu Nov 17, 2005 8:02 am Post subject: Re: template metaprogramming |
|
|
Mark wrote:
| Quote: | Erik Wikström wrote:
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 Harrison wrote:
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...
|
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.
| Quote: |
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.
| Quote: |
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?
|
You were so confident that you were right, I thought you must have tried
to compile something.
john
|
|
| Back to top |
|
 |
peter koch Guest
|
Posted: Thu Nov 17, 2005 11:00 pm Post subject: Re: template metaprogramming |
|
|
John Harrison skrev:
| Quote: | Mark wrote:
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.
|
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
| Quote: |
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
|
|
|
| Back to top |
|
 |
Mark Guest
|
Posted: Fri Dec 02, 2005 2:45 am Post subject: Re: template metaprogramming |
|
|
oh. well then. i guess i learned something about my compiler today
very nifty.
thanks guys. so there was a better solution than RGB<1,2,3>::val after
all.
| Quote: | 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.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|