473,397 Members | 2,116 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,397 software developers and data experts.

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

Nov 22 '05 #1
12 1327
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

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
Nov 22 '05 #2
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"???

Nov 22 '05 #3
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...@nyc.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?

Nov 22 '05 #4
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
--
"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
Nov 22 '05 #5
Erik Wikström wrote:
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
Nov 22 '05 #6
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.

Nov 22 '05 #7
On 2005-11-16 19:02, 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.

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
Nov 22 '05 #8
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.


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
Nov 22 '05 #9
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...

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?

Nov 22 '05 #10
Mark wrote:
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.

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.


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
Nov 22 '05 #11

John Harrison skrev:
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

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


Nov 22 '05 #12
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.
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.

Dec 2 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave | last post by:
For those who might be so inclined, I was wondering if I might get honest critiques of my first real venture into template metaprogramming. This template metaprogram sorts a list of integers at...
12
by: Dave | last post by:
Would people agree with the statement that to a large degree, using template metaprogramming techniques turns a C++ compiler into a C++ interpreter (but just for the metaprogrammed portions of the...
5
by: Mohammad | last post by:
Hi, Is it possible to disable a method of a template class depending on the typename at compile time? thanks!
21
by: Protoman | last post by:
I've been looking at template metaprogramming. It seems really cool, make the compiler do most of the work. I have very simple program that uses TMP,it calculates the square of a number, but it...
5
by: Mark Stijnman | last post by:
I am trying to teach myself template metaprogramming and I have been trying to create lists of related types. I am however stuck when I want to make a template that gives me the last type in a...
7
by: Joe | last post by:
Hi, I found a concept named template metaprogramming that can be used in C+ + code at compile-time. I am a beginner at C++. But I am a programmer on the .NET platform. Do you know if template...
1
by: Ted | last post by:
I have cross posted this to comp.lang.c++ and to sci.math.num- analysis in the belief that the topic is of interest to some in both groups. I am building my toolkit, in support of my efforts in...
5
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.