• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Endemoniada

C++ Macro Problem

14 posts in this topic

Hi guys, check this out:

struct float2
{
float x;
float y;
};

#define set2(v,_x,_y) {v->x=_x; v->y=_y;}

// the following won't work because the curly braces don't get expanded:

if(a == 1)
set2(&v,1.0f,1.0f);
else
set2(&v,0.0f,0.0f);

// illegal else without matching if

I know I can put the curly braces in the conditional but is there any way I can get it to work as is ?

Thanks.
0

Share this post


Link to post
Share on other sites
[quote name='Endemoniada' timestamp='1307582717' post='4821156']
I know I can put the curly braces in the conditional but is there any way I can get it to work as is ?
[/quote]
There's no better way than using a trivial loop or a conditional. See [url="http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.5"]What should be done with macros that have multiple lines?[/url]
1

Share this post


Link to post
Share on other sites
Expand you macro and look at what's produced:[code] if (a == 1)
{&v->x= 1.0f; &v->y=1.0f;}
;
else
{&v->x= 0.0f; &v->y=0.0f;}
;[/code]Your problem has nothing to do with braces -- the problem is semi-colons.

But as jyk said above, there's no reason to use a [font="Courier New"]#define[/font] for this when an [font="Courier New"]inline[/font] function will suffice.
1

Share this post


Link to post
Share on other sites
Others had given the answer.

One more thing, VC can (maybe GCC also can) generate .i files which is expanded by preprocessor, so you can see how the macro is expanded.

But more important, be careful to use macro, only use macro for magic things. Always prefer functions and constants to macros.
0

Share this post


Link to post
Share on other sites
[quote name='jyk' timestamp='1307584270' post='4821166']
Any reason you're not just using a function?
[/quote]

I'm guessing it's a theoretical question. The practical reason is if you needed to inline functions in older (e.g. C89 and K&R C) code.
1

Share this post


Link to post
Share on other sites
[quote name='Steno' timestamp='1307586325' post='4821178']
I'm guessing it's a theoretical question. The practical reason is if you needed to inline functions in older (e.g. C89 and K&R C) code.[/quote]
Only the OP can tell us for sure, but I get the impression the question is practical in nature. For one thing, the thread title is 'C++ Macro Problem', which suggests the question is specifically in reference to C++ rather than C.

I could be wrong though (maybe the OP will clarify).
2

Share this post


Link to post
Share on other sites
Hi guys,

It's practical in nature. I read in a few places that inline isn't guaranteed so I thought macros for these simple operations would be better. I'm starting to think I should be using funtions though.

Thanks for all the info.

PS - I'm using VS2008
0

Share this post


Link to post
Share on other sites
With MSVC you can use __forceinline which will either inline the function or generate a level 1 warning when the function can't be inlined.
0

Share this post


Link to post
Share on other sites
[source lang="cpp"]
struct float2
{
float x,y;
void set(float a,float b){ x=a; y=b; }
};
[/source]

Declaring and defining the method within the class automatically implies inline. If you are optimizing for speed in your release build, it is a far better idea normally to allow the compiler to decide whether to inline or not, unless you have profiled and proven a bottleneck.
0

Share this post


Link to post
Share on other sites
[quote]
It's practical in nature. I read in a few places that inline isn't guaranteed so I thought macros for these simple operations would be better.
[/quote]
Just because it isn't guaranteed doesn't mean it isn't happening. Such a trivial function will almost certainly be inlined.

As Asrdvajk says, only profiling can indicate if the decision to inline or not is justified. Remember than forcing inlining can cause code bloat, which can slow things down instead of speeding them up.
1

Share this post


Link to post
Share on other sites
[quote name='wqking' timestamp='1307585645' post='4821174']
Others had given the answer.

One more thing, VC can (maybe GCC also can) generate .i files which is expanded by preprocessor, so you can see how the macro is expanded.

But more important, be careful to use macro, only use macro for magic things. Always prefer functions and constants to macros.
[/quote]

Just curious, which word is wrong to get down vote?
Voting worth nothing but unreasonable down vote is not friendly.
0

Share this post


Link to post
Share on other sites
[quote name='wqking' timestamp='1307800539' post='4822075']
Just curious, which word is wrong to get down vote?
Voting worth nothing but unreasonable down vote is not friendly.
[/quote]

One errant down vote shouldn't be taken too seriously. I would *guess* that it was your last line: "But more important, be careful to use macro,[b] only use macro for magic things[/b]. Always prefer functions and constants to macros." This seems to support using macros for bizarre, unusual, unreadable, and surprising code. Generally the better advice is to avoid anything "magical" in the first place. However, I think you were just trying to say to use them when the only alternatives are going to be really messy.

BTW, I gave you an upvote since your .i file suggestion is a good one.
0

Share this post


Link to post
Share on other sites
[quote name='Ezbez' timestamp='1307801182' post='4822079']
One errant down vote shouldn't be taken too seriously. I would *guess* that it was your last line: "But more important, be careful to use macro,[b] only use macro for magic things[/b].
[/quote]

Macro can do magic things for very useful cases, one case is simulating variadic template parameters. Boost library uses it. Boost also used macro extensively for some magic things which the library users will never aware.
Carefully using macro will really get very amazing powerful use. I used macros to make a callback system.
That's I called magic.
Functions and constants are not magic, so don't use macros for them.

[quote name='Ezbez' timestamp='1307801182' post='4822079']
BTW, I gave you an upvote since your .i file suggestion is a good one.
[/quote]

Thanks.
I really hope gdnet won't go stackoverflow way, full of up/down vote and a lot of votes are quite bias (GCC fans may down vote any one recommending VC, etc) and subjective. (no argument on this point because it's off topic).

Edited:
Again i don't mind down vote, but please jump out to give the reason, thanks.
0

Share this post


Link to post
Share on other sites
Downvotes corrected. No idea what that was about. We are all aware that there are certain things you can only do with macros in C++.

My personal rule of thumb is: If it can be done without a macro, do it the other way, otherwise suck it up or move to a more modern language.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0