Macros

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 30, 2003 at 03:25

I was just studying Macros and they are pretty useful … Just want to know how to create a Macro to a function that returns a value? :blush:

29 Replies

Please log in or register to post a reply.

Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 Sep 30, 2003 at 03:34

Are you talking about C/C++ macros?

If yes, then here is an example:

#define sum(a,;)   a+b

macros don’t return any value..they simply get replaced (like doing a search and replace) with the macro definition.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 30, 2003 at 04:45

hm.. macros are evil.. just google for that:D

they can be useful, though.

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 30, 2003 at 05:09

I meant if I have a function like the one below :

void Sum(int a, int b)

Then I can use it by the following Macro

#define SUM(i, j) Sum (i, j)

But if the function is returning an integer (or anyother value) like

int Sum(int a, int b)

How can I use macro with this function returning a value :blush: ?

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Sep 30, 2003 at 05:41

first of all… why would you want to do this ???
also the macro does nothing more than replacing the text SUM(i, j) with Sum(i, j)
so it doesn’t matter if the function returns a value or not. again, why would you want to do that ???

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 30, 2003 at 08:04

for example … take a look at the following code :

HWND WinCreate (LPSTR Title, int Width, int Height)
{
 ...
 ...
 ...

 return hWindow;
}

I can use the following macro for this function :

#define WINCREATE_DEFAULT(Title) WinCreate (Title, 800, 600)

The above idea can be used for a pretty handly little stuffs … I think :blush: !

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Sep 30, 2003 at 08:36

erm, there is such a thing as standard parameter values…

int WinCreate(char* title, int width = 800, int height = 600);

i think what you want to do will just make you code uncomprehendable…

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 30, 2003 at 09:58

I want to do this to add different values for rwidth & height like 600 x 400, 800 x 600 or 1024 x 800.

Microsofot people have used these type of Macors in DirectX.

An example is for the Color. There is one with the ARGB where we enter 4 vales, and the other is RGBA where we add only 3 values. The Alpha (A) is entered in the Macro … :unsure:

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 30, 2003 at 10:03

does that make it good? no. learn how to use makros and you’d realise you DON’T use it for that.

for constants, you use enums or const, for small functions like those named colourconversions, you use inline functions. there’s only ONE need for macros.. wich i won’t tell you but YOU will tell ME! to show you understand them

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 30, 2003 at 10:22

@davepermen

there’s only ONE need for macros.. wich i won’t tell you but YOU will tell ME! to show you understand them

:huh: You just sond like my teacher … :blink:

I think it is good if we use it as follows:

#define ERRORMSG(String) MessageBox (NULL, String, "Error", MB_OK | MB_ICONERROR)

what do you think dave … I will really love to know your suggestion :lol: :blush: !

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 30, 2003 at 11:22

what do you gain from this?

use

inline void MsgBox(const char* string) {
  MessageBox(0,string,"MsgBox without Stupid Macro",MB_OK...);
}

instead. if you really _NEED_ that.

no, it is NOT the good use of macro.

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Sep 30, 2003 at 11:33

:yes: OK Dave, I got your point.

Most of people don’t prefer using MACROS as functions. Is it good to use it to define values right or not? It can also be achieved by declaring variables… :confused:

But why do Microsoft people use it so much? :confused:

0684f9d33f52fa189aad7ac9e8c87510
0
baldurk 101 Sep 30, 2003 at 17:46

I’d rather create a macro for the MessageBox example than an extra function, but here is the important point.

C programmers use macros, C++ programmers don’t. A good C++ programmer would never need a macro. For defining constants he would use const.

But why do Microsoft people use it so much?

I’m sorry if this surprises you, but Microsoft don’t necessarily write good or clean code.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 30, 2003 at 20:38

the win32 api is from the times before you even all knew programming exists:D it is old, designed for c, and all..

baldurk. there is no point in using macros and you know it. none, except one..

i have had enough troubles to learn that windows.h defines both max and min as macros.. guess how i wondered as i used the same names in my code..

22b3033832c5c699c856814b0cf80cb1
0
bladder 101 Oct 01, 2003 at 01:25

@davepermen

i have had enough troubles to learn that windows.h defines both max and min as macros.. guess how i wondered as i used the same names in my code..

ooh. I remember those days. Damn windows.h drove me insane. Took me a while to figure out that you had to #define NOMINMAX before including windows.h.

But macros do come in handy sometimes. Most of the times macros are just used to make typing less though, which is not good, generally speaking. But there are times when they are used really well. For example, has anyone used the memory manager from http://www.fluidstudios.com. I dont think there is a way to accomplish what he does any other way then to use macros for new/delete

Yes there are disadvantages to dropping his mem manager in your project, but the adavantages are way more in this case.

Also there are somethings you cant do with normal functions but you can do with macros. Like the # and ## thingies (dont know what they are called exactly). I guess macros are usually used for debugging purposes. This is an amazing use of macros for example

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Oct 01, 2003 at 01:39

dave, what do you think Macros are actualy used for? :7

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Oct 01, 2003 at 04:29

looks like you don’t inform yourself well, do you?

well, read up the post right in front of yours.. there are good examples there.

in general, macros are ONLY good if you need them. this is true for example in debugging situations, where a simple assert can get much additional info thanks to a macro. like this simple snippet:

#define assert(assertion) doAssert(assertion,#assertion,__LINE__,__FILE__)

etc.. the cuj assert code is much more complex, but as well much more powerful.

once you defined assert, it acts like a keyword to the language, means you CAN NOT USE IT IN ANY OTHER WAY EVER AGAIN. and that is the worst thing of macros. as you can see by the little reminder to min and max and windows.h

overloading new and delete for memory managing/debugging purposes works well without macros, but normally, you want to have there some logging info like __LINE__ and __FILE__ in, too. so you have to use some macro-way to get them automatically in again. no big problem, because new and delete yet ARE keywords. still, it gives issues..

the last way, while not used much anymore, is the way to help doing less typing. something like a

#define var(name) ConsoleVariable name(#name)

var(screenWidth);
var(screenHeight);
var(colourDepth);

...

var(ammo);

#undef var // don't forget this.. except var is a new language keyword for you wich you want to use everywhere..

then again, you could do this with a simple

std::map<std::string,ConsoleVariableStorage> consoleVariables;

consoleVariables["screenWidth"] = someWidth; ...

and then load them directly from some config files anyways..

this would mean more runtime overhead, as you fill the map at runtime, but it means, too, less globals, no in-code management (==much more flexible, just add variables in the config file..), and, last but not least, no macro.

yes i DO (or did, now i’m coding solely in d currently) use macros. but when i use them, then the “right way”. that means for example an assert macro, wich get compiled to {} if you do a releasebuilt, and a verify macro, same as assert, but as well in the releasebuilt. and THEN, i go to usertype.dat for my visual studio, and add assert and verify as keywords in there, so they get blue highlighted as every other c++ keyword does.

and _THEN_ i’m on the save side again.

the chaotic way c and c++ are built, and combined made me learn a lot about both languages, and it made me learn that they can get used and abused in EXTREME ways (check out the boost preprocessor lib.. hehehe macros pure). they teached me as well to _THINK_ while i’m coding, to _PLAN_ when i code, and always rethink, when ever i write something, if that thing can be really useful that way, or if it only fits my needs for now.

macros are very dangerous, they don’t follow any c or c++ language rules. don’t use them for constants, don’t use them for small functions, at best, don’t use them at all. then you’re on the save side..

E05263ec846eb85da803f56e2917962d
0
Noor 101 Oct 01, 2003 at 04:34

Good to see you back davepermen. where have you been? Long time no see.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Oct 01, 2003 at 04:44

i needed to take a breath.. i was always here, just not posting..

(and yes, it was because of anja that i needed to take the breath, and no i am not over it because yes i still love her and miss her very much.. but i finally got energy again to post around here..)

uhm, had a lot to do. tons of times i was out with friends and chicks (hehehe:D), i have currently two turntables and a mixer at home, so i’m more into music currently, too.. and i’m diving into d.. it starts to get great, the compiler starts to support its own language rather well, can’t wait for a final one. currenlty, anubis and i have a lot of stress with the compiling/linking itself, but that’ll be over soon i hope (a future compiler release..)..

if you want, there is some small code you can try from me..

on D.rar.. i call them tutorials to feel good:D

btw, to not get compiler errors, unrar it to some folder with NO spaces in the full path name.. say onto c:\downloads\d or c:\d or what ever.. and then you can just run the batch file to compile. have fun… more to come…..

40da7e74fdca33768b6714646853b3f5
0
TheLionKing 101 Oct 01, 2003 at 05:18

Wooooooowwwwwwwwww! :D

Got it dave :yes: !

I know you were busy … there was someone else chatting on hotmail at your place :blush: !

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Oct 01, 2003 at 14:15

i think i’ll add them into the snippets in one or the other way.. i just could not get arsed..

the time i code more, and feel more happy, i’ll post around codesnippets again. currently, i just don’t know..

well well.. i’ve rewritten the raytracer in tutorial 5, supports now an arbitary amount of objects…… well… spheres:D and all in the same colour, yeah:D

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Oct 01, 2003 at 15:41

looks like you don’t inform yourself well, do you?

sometimes i get the feeling that you lack a certain amount of respect for other people…. you are right about macros though. my offer to team up on a few D tutorials is still there. what am i talking, we see each other every day anyway…

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Oct 01, 2003 at 16:23

if you would reply, yeah:D but i guess the voyage back home took you a lot of energy or so..

sometimes i have a lack of respect yes. to people who don’t care about the answers directly, yes. can be disturbing sometimes, but its my way…

i’m sorry btw if i hurt anyone. THAT was not planned..

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Oct 01, 2003 at 19:06

i don’t think you hurt anyone… we know you all the way you are.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Oct 01, 2003 at 20:01

looks like i’ve hurt at least one too much.. (or two, me included…)…..

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Oct 01, 2003 at 22:40

tutorial5.jpg

pict of tutorial5..

i’ll still callem tutorials:D they have about nothing to do with it:D but hey.. anubis can write text to them with my help i guess… together we’re strong! :D

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Oct 02, 2003 at 09:50

no comment… :Dtutorial6.jpg

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Oct 02, 2003 at 11:14

i still find no words for the disgust i feel when i see somebody using the standard windows skin. *shiver*
dosn’t it hurt your eyes ???

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Oct 02, 2003 at 11:29

nope, i actually like it quite much..

[img]http://www.itstudents.ch/users/dave/files/tutorial7playingwithfresnel.JPG[/img]

playing with the fresnel term, in this case a negative one, making dark spheres glow:D btw, its fun to see how it negates each other reflection.. white,black,white,black.. i just love that reddish colour that happens when you add negative brightness to orange.. :D

if you want to see something REALLY discusting, download dev-c++, and enable the standard skin. it doesn’t accept the skin, and the menues get white.. looks SO UGLY! :D

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Oct 02, 2003 at 13:23

tutorial8.jpg

a new object included (and a second trace method for shadowrays.. will get replaced by an optimized one later:D)