473,804 Members | 3,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alternatives to #define?

What other c++ constructs can I use instead of #define for executing a
couple of functions?
Example:

#define DO_STUFF doThis(); doThat();

I'd guess that I can either use a template function, an inlined function or
an inlined static method.

//1
namespace MyUtils
{
template<>
void doStuff()
{
doThis();
doThat();
}
}

//2
namespace MyUtils
{
inline void doStuff()
{
doThis();
doThat();
}
}

//3
class MyUtils
{
public:
static inline void doStuff() const
{
doThis();
doThat();
}
}

I *believe* that the template version always is inlined, and that the other
2 versions is probably inlined.
Are theese approaches correct?
Which should be preferred?
Are there any other better way?

/Carl
Jul 22 '05
14 4464
Robbie Hatley wrote:
"Carl Ribbegaardh" <ca************ *********@hotma il.com> wrote:

#define DO_STUFF doThis(); doThat();

I actually like that. Simple, direct, ALWAYS inlined.


It's dangerous, though. What if you have code like:

if (condition)
DO_STUFF
else
do_something_el se();

This won't expand properly. You should write the macro as:

#define DO_STUFF {doThis(); doThat();}

but IMO using an inlined function would be better.

--
Mike Smith
Jul 22 '05 #11
On Mon, 05 Jul 2004 12:13:55 +0200, Carl Ribbegaardh wrote:
"Mark A. Gibbs" <x_*********@ro gesr.com_x> wrote in message
news:rq******** ******@news04.b loor.is.net.cab le.rogers.com.. .


One more question on the subject:

Why is (for example) SUCCEEDED and FAILED (in winerror.h) implemented as
macros?
..and other stuff that's common like SAFE_DELETE and so on...

Is it just done by habit, or are there any other reason for not using inline
functions?


The standard windows headers can be used in either C or C++ programs. And
inline functions are a C++-only nicety...

- Jay

Jul 22 '05 #12
Jay Nabonne wrote:
On Mon, 05 Jul 2004 12:13:55 +0200, Carl Ribbegaardh wrote:
"Mark A. Gibbs" <x_*********@ro gesr.com_x> wrote in message
news:rq******** ******@news04.b loor.is.net.cab le.rogers.com.. .


One more question on the subject:

Why is (for example) SUCCEEDED and FAILED (in winerror.h) implemented
as macros?
..and other stuff that's common like SAFE_DELETE and so on...

Is it just done by habit, or are there any other reason for not using
inline functions?


The standard windows headers can be used in either C or C++ programs.
And inline functions are a C++-only nicety...


That's not true. ISO C has been knowing the inline keyword for 5 years
now, and many C compilers have been supporting it for a much longer
time. The windows headers have never been ISO compliant anyway, so they
could have used inline, too.

Jul 22 '05 #13
On Thu, 08 Jul 2004 11:12:18 +0200, Rolf Magnus wrote:
Jay Nabonne wrote:
On Mon, 05 Jul 2004 12:13:55 +0200, Carl Ribbegaardh wrote:
"Mark A. Gibbs" <x_*********@ro gesr.com_x> wrote in message
news:rq******** ******@news04.b loor.is.net.cab le.rogers.com.. .
One more question on the subject:

Why is (for example) SUCCEEDED and FAILED (in winerror.h) implemented
as macros?
..and other stuff that's common like SAFE_DELETE and so on...

Is it just done by habit, or are there any other reason for not using
inline functions?


The standard windows headers can be used in either C or C++ programs.
And inline functions are a C++-only nicety...


That's not true. ISO C has been knowing the inline keyword for 5 years
now, and many C compilers have been supporting it for a much longer
time. The windows headers have never been ISO compliant anyway, so they
could have used inline, too.


I had a feeling someone would object to that.

How about this replacement for my last line then: at the time that the
Windows header files were developed (> 5 years ago), C compilers (in
particular the Microsoft C compiler) didn't support inline functions.

Does that sit well enough? :)

- Jay

Jul 22 '05 #14
"Jay Nabonne" <ja*@rightagain BYTEME.com> wrote in message
news:pa******** *************** ****@rightagain BYTEME.com...
On Thu, 08 Jul 2004 11:12:18 +0200, Rolf Magnus wrote:
Jay Nabonne wrote:
On Mon, 05 Jul 2004 12:13:55 +0200, Carl Ribbegaardh wrote:

"Mark A. Gibbs" <x_*********@ro gesr.com_x> wrote in message
news:rq******** ******@news04.b loor.is.net.cab le.rogers.com.. .
>

One more question on the subject:

Why is (for example) SUCCEEDED and FAILED (in winerror.h) implemented
as macros?
..and other stuff that's common like SAFE_DELETE and so on...

Is it just done by habit, or are there any other reason for not using
inline functions?
The standard windows headers can be used in either C or C++ programs.
And inline functions are a C++-only nicety...


That's not true. ISO C has been knowing the inline keyword for 5 years
now, and many C compilers have been supporting it for a much longer
time. The windows headers have never been ISO compliant anyway, so they
could have used inline, too.


I had a feeling someone would object to that.

How about this replacement for my last line then: at the time that the
Windows header files were developed (> 5 years ago), C compilers (in
particular the Microsoft C compiler) didn't support inline functions.

Does that sit well enough? :)

- Jay


Now I understand better why the API's looks like they do. Thanks a lot! :-D

/Carl
Jul 22 '05 #15

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

Similar topics

31
2089
by: CYBER | last post by:
Hello Is there any other way under python to create blocks ?? instead of def sth(x): return x
9
1688
by: Simon Elliott | last post by:
If I have a base class and several possible derived classes, and I have a pointer to the base class, what's the best way of determining whether the pointer is pointing to a base class or to a derived class? The classes have virtual functions, so I could use a dynamic_cast. Is there much overhead associated with this? Alternatively I could have an instance variable in the base class which derived classes are required to set, or a...
2
1752
by: Michael | last post by:
Ok Guys, on the same thread I'm writing a Game engine, which performs collision detection. I want to sepate the collision boundings libaray from the main game engine and load it as a library. If simplified this example but it illustrates the point.... :-) So i declare an abstract base class ( & function ): ///////////////////////////////////////////////////
0
1482
by: Imre | last post by:
I'm planning to create a macro-based property system (reflection, automatic serializing for properties, that kind of stuff). The system would involve writing some PROPERTY(propName) macros between a BEGIN_CLASS(ThisClass) and an END_CLASS() macro. The PROPERTY macros should somehow know ThisClass, and that's where it start to get tricky. A #define ThisClass would of course help, but I'd rather make it part of the BEGIN_CLASS macro, and...
43
5031
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own libraries. I'm not sure if that's a good thing or not. AFAIK, most of Qt is compatable with the Standard Library. That is, QLT can interoperate with STL, and you can convert back and forth between std::string and Qt::QString, etc. Are there any...
1
1299
by: Doug | last post by:
Looking for opinions/suggestions: Suppose I have a "region" of an aspx page I want to hide or show based on whatever runtime conditions. Additionally, the entire region is defined by an HTML <TABLE>. There is nothing else in the region beyond whatever is contained in the <TABLE>. I see at least two options for showing/hiding this region. 1. Wrap the table in an <ASP:Panel> and set the panel's Visible property to
6
2159
by: greek_bill | last post by:
Hi, I'm interested in developing an application that needs to run on more than one operating system. Naturally, a lot of the code will be shared between the various OSs, with OS specific functionality being kept separate. I've been going over the various approaches I could follow in order to implement the OS-specific functionality. The requirements I have are as follows :
4
1292
by: sunderjs | last post by:
Hi, This is from a typical telecom software implementation. I have three subsystems (x, y, z) which exchange data amongst them. The arrangement is such that x talks to y over interface xy. y subsystem them talks to z over yz interface. In a typical scenario, y would receive a set of parameters from x (over xy). Some of these are meant for z subsys as well. So y needs to send these plus some more parameters to z. The implementation...
0
2535
by: howa | last post by:
Hello Besides YUI grid / builder (http://developer.yahoo.com/yui/grids/ builder/), are there any alternatives such that it provides a very flexible way to define site grid yet conform to the web standard? I just want to compare them if you got any good suggestions. Thanks.
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10075
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.