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

Home Posts Topics Members FAQ

MACROs are ugly but...

....in certain cituations they can be useful if not for anything else,
then at least for saving a lot of repetetetetetit ititive typing. :-)

Beyond the point of "do something better instead", I'm curious about how
the following syntactical problem can be solved. It should apply equally
to C and C++ as it mainly is a preprocessor-related problem.

I tryed to define something similar to the following example:

-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning( disable : 4995 ) /* Annoying warning */ \
\
/* Do some dirty work here (that I want to hide) */ \
\
#pragma warning( default : 4995 ) /* Warning reactivated */ \
/* finished dirty part */
-------------------------------------

The point was of course to use that DO_DIRTYWORK several places around
the sources. Its content was not really part of the main logic, but
merely a library workaround that required some lines of code injected
"everywhere ". (When the library was fixed later, then maybe I could just
redefine the DO_DIRTYWORK macro as empty!)

However, this doesn't compile of course, because a prepocessor directive
needs to start as the first non-blank # on a line, which is conflicting
with the macro's end-of-line escapes (the \). Obviously, what I _wanted_
to achieve in this particular case, was to have the actual preprocessor
directives emitted from the preprocessor's first replacement (then
further preprocessed in-place where the actual code is used).

Can something like that be achieved?
How?
--
-+-Ben-+-
Oct 6 '05 #1
11 2862

Ben Hetland wrote:
...in certain cituations they can be useful if not for anything else,
then at least for saving a lot of repetetetetetit ititive typing. :-)

Beyond the point of "do something better instead", I'm curious about how
the following syntactical problem can be solved. It should apply equally
to C and C++ as it mainly is a preprocessor-related problem.

I tryed to define something similar to the following example:

-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning( disable : 4995 ) /* Annoying warning */ \
\
/* Do some dirty work here (that I want to hide) */ \
\
#pragma warning( default : 4995 ) /* Warning reactivated */ \
/* finished dirty part */
-------------------------------------

The point was of course to use that DO_DIRTYWORK several places around
the sources. Its content was not really part of the main logic, but
merely a library workaround that required some lines of code injected
"everywhere ". (When the library was fixed later, then maybe I could just
redefine the DO_DIRTYWORK macro as empty!)

However, this doesn't compile of course, because a prepocessor directive
needs to start as the first non-blank # on a line, which is conflicting
with the macro's end-of-line escapes (the \). Obviously, what I _wanted_
to achieve in this particular case, was to have the actual preprocessor
directives emitted from the preprocessor's first replacement (then
further preprocessed in-place where the actual code is used).

Can something like that be achieved?
How?
--
-+-Ben-+-


How about putting the "dirtywork" in a separate header, and doing this:

#if defined(DO_DIRT YWORK)
#include "dirtywork. h"
#endif

Then have your pragmas and whatnot in your dirtywork.h...

-David

Oct 6 '05 #2
[cross-posting removed]

On 6 Oct 2005 07:06:30 -0700, "David Resnick" <ln********@gma il.com>
wrote:

Ben Hetland wrote:
...in certain cituations they can be useful if not for anything else,
then at least for saving a lot of repetetetetetit ititive typing. :-)

Beyond the point of "do something better instead", I'm curious about how
the following syntactical problem can be solved. It should apply equally
to C and C++ as it mainly is a preprocessor-related problem.

I tryed to define something similar to the following example:

-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning( disable : 4995 ) /* Annoying warning */ \
\
/* Do some dirty work here (that I want to hide) */ \
\
#pragma warning( default : 4995 ) /* Warning reactivated */ \
/* finished dirty part */
-------------------------------------

The point was of course to use that DO_DIRTYWORK several places around
the sources. Its content was not really part of the main logic, but
merely a library workaround that required some lines of code injected
"everywhere ". (When the library was fixed later, then maybe I could just
redefine the DO_DIRTYWORK macro as empty!)

However, this doesn't compile of course, because a prepocessor directive
needs to start as the first non-blank # on a line, which is conflicting
with the macro's end-of-line escapes (the \). Obviously, what I _wanted_
to achieve in this particular case, was to have the actual preprocessor
directives emitted from the preprocessor's first replacement (then
further preprocessed in-place where the actual code is used).

Can something like that be achieved?
How?
--
-+-Ben-+-


How about putting the "dirtywork" in a separate header, and doing this:

#if defined(DO_DIRT YWORK)
#include "dirtywork. h"
#endif

Then have your pragmas and whatnot in your dirtywork.h...

-David


This is a pretty good solution for configuration macros which are
defined for an entire project, or for a big part of one.

Unfortunately, it isn't quite as easy as that. Often one would want to
wrap only single functions, or perhaps the contents of a single source
file (not a header!) between #pragma warning (disable ... ) and
#pragma warning ( default ... ) or something similar. This would
require at least two separate header files, and I think the
maintenance involved would be greater than just typing in the
pragma's.

IMHO one should only put such things into separate files which need to
be accessed by more than one source file. One would hardly wish to
turn off specific warnings for an entire project, although there are
certainly enough programmers who would argue otherwise. I, for one, am
only happy when my code compiles without any warnings at all. If there
is a certain warning I cannot work around, then I will turn it off
only for whatever portion of code causes the warning and document the
place well as to why it should be turned off. Typing the #pragma or
whatever directly into the source enforces the idiom that they are
extremely local, and that there is a strong dependency between them
and the code they influence.

At any rate, commonly-used #pragmas can easily be stored as templates
in most modern-day IDE's to save typing.

--
Bob Hairgrove
No**********@Ho me.com
Oct 6 '05 #3

Ben Hetland wrote:
...in certain cituations they can be useful if not for anything else,
then at least for saving a lot of repetetetetetit ititive typing. :-)

Beyond the point of "do something better instead", I'm curious about how
the following syntactical problem can be solved. It should apply equally
to C and C++ as it mainly is a preprocessor-related problem.

I tryed to define something similar to the following example:

-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning( disable : 4995 ) /* Annoying warning */ \
\
/* Do some dirty work here (that I want to hide) */ \
\
#pragma warning( default : 4995 ) /* Warning reactivated */ \
/* finished dirty part */
-------------------------------------

The point was of course to use that DO_DIRTYWORK several places around
the sources. Its content was not really part of the main logic, but
merely a library workaround that required some lines of code injected
"everywhere ". (When the library was fixed later, then maybe I could just
redefine the DO_DIRTYWORK macro as empty!)

However, this doesn't compile of course, because a prepocessor directive
needs to start as the first non-blank # on a line, which is conflicting
with the macro's end-of-line escapes (the \). Obviously, what I _wanted_
to achieve in this particular case, was to have the actual preprocessor
directives emitted from the preprocessor's first replacement (then
further preprocessed in-place where the actual code is used).

Can something like that be achieved?
How?


You should be able to use the _Pragma preprocessor operator to create
#pragma directives using macros. _Pragma is part of the C99 standard
and I am unsure of its current status in C++. But many C++ compilers
implement it.

Greg

Oct 6 '05 #4
David Resnick wrote:
How about putting the "dirtywork" in a separate header, and doing this:

#if defined(DO_DIRT YWORK)
#include "dirtywork. h"
#endif


Yes, that might work, but means 3 lines of code for every single of
these places that I need this thing. I think that is just contributing
more to "polluting" the general code structure/algorithm wherever it's
used. BTW, if it's only the one pragma, then it could also be achieved
without the extra include by writing

#pragma ( disable ... )
DO_DIRTYWORK
#pragma ( default ... )

everywhere. IMHO, it's still a bit too messy.
(However, the real-life case had more than that...)

I guess I cannot even put it all into an include file, and have the
macro #include that, because of the # again...

--
-+-Ben-+-
Oct 6 '05 #5
Ben Hetland wrote:
...in certain cituations they can be useful if not for anything else,
then at least for saving a lot of repetetetetetit ititive typing. :-)

Beyond the point of "do something better instead", I'm curious about how
the following syntactical problem can be solved. It should apply equally
to C and C++ as it mainly is a preprocessor-related problem.

I tryed to define something similar to the following example:

-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning( disable : 4995 ) /* Annoying warning */ \
\
/* Do some dirty work here (that I want to hide) */ \
\
#pragma warning( default : 4995 ) /* Warning reactivated */ \
/* finished dirty part */
-------------------------------------

The point was of course to use that DO_DIRTYWORK several places around
the sources. Its content was not really part of the main logic, but
merely a library workaround that required some lines of code injected
"everywhere ". (When the library was fixed later, then maybe I could just
redefine the DO_DIRTYWORK macro as empty!)

However, this doesn't compile of course, because a prepocessor directive
needs to start as the first non-blank # on a line, which is conflicting
with the macro's end-of-line escapes (the \). Obviously, what I _wanted_
to achieve in this particular case, was to have the actual preprocessor
directives emitted from the preprocessor's first replacement (then
further preprocessed in-place where the actual code is used).

Can something like that be achieved?
How?
--
-+-Ben-+-


A small criticism: if possible, it would be better to save and restore
the state of the warnings rather than set them to the default since the
warnings may have already enabled or disabled explicitly elsewhere.
With VC++ this can be done with something like this:

#pragma warning(push)
#pragma warning(disable :4995)
// Code that generates 4995 goes here
#pragma warning(pop)

Cheers! --M

Oct 6 '05 #6


Ben Hetland wrote On 10/06/05 09:59,:
...in certain cituations they can be useful if not for anything else,
then at least for saving a lot of repetetetetetit ititive typing. :-)

Beyond the point of "do something better instead", I'm curious about how
the following syntactical problem can be solved. It should apply equally
to C and C++ as it mainly is a preprocessor-related problem.
#pragma looks like a preprocessor directive, and the C
Standard describes it in the preprocessor section. If you
think about it, though, it's immediately clear that most
#pragmas are not "really" preprocessor constructs, because
their effect is felt long after the preprocessor has been
and gone. A #pragma that adjusts struct padding or controls
the aggressiveness of the optimizer's loop unrolling has
nothing to do with the preprocessor and everything to do
with code generation.

The point of all this is that the way #pragma is handled
may very well differ between C and C++. Since my knowledge
of C++ wouldn't even earn me a D- I'll answer only for C;
someone else may be able to help you with That Other Language.
I tryed to define something similar to the following example:

-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning( disable : 4995 ) /* Annoying warning */ \
\
/* Do some dirty work here (that I want to hide) */ \
\
#pragma warning( default : 4995 ) /* Warning reactivated */ \
/* finished dirty part */
-------------------------------------

The point was of course to use that DO_DIRTYWORK several places around
the sources. Its content was not really part of the main logic, but
merely a library workaround that required some lines of code injected
"everywhere ". (When the library was fixed later, then maybe I could just
redefine the DO_DIRTYWORK macro as empty!)

However, this doesn't compile of course, because a prepocessor directive
needs to start as the first non-blank # on a line, which is conflicting
with the macro's end-of-line escapes (the \). Obviously, what I _wanted_
to achieve in this particular case, was to have the actual preprocessor
directives emitted from the preprocessor's first replacement (then
further preprocessed in-place where the actual code is used).
The real reason this doesn't work is that expanding
a macro never produces a preprocessor directive, not even
if the expansion happens to resemble one. (6.10.3.4/3)
Can something like that be achieved?
How?


If your compiler supports the latest "C99" Standard, you
can use the _Pragma operator (6.10.9):

#define DO_DIRTYWORK \
_Pragma("warnin g( disable : 4995 )") \
dirty_little_do ings_going_on \
_Pragma("warnin g( default : 4995 )")

Failing that, the best I can suggest is to put the
dirty work in a separate function, and have DO_DIRTYWORK
generate a call to it.

--
Er*********@sun .com

Oct 6 '05 #7
* Ben Hetland:
...in certain cituations they can be useful if not for anything else,
then at least for saving a lot of repetetetetetit ititive typing. :-)

Beyond the point of "do something better instead", I'm curious about how
the following syntactical problem can be solved. It should apply equally
to C and C++ as it mainly is a preprocessor-related problem.
Your abstraction of your proposed _solution_ of the problem is only
preprocessor-related, but whether problem is, is another question.

I tryed to define something similar to the following example:

-------------------------------------
#define DO_DIRTYWORK \
\
#pragma warning( disable : 4995 ) /* Annoying warning */ \
\
/* Do some dirty work here (that I want to hide) */ \
\
#pragma warning( default : 4995 ) /* Warning reactivated */ \
/* finished dirty part */
-------------------------------------


Macros can't generate macros.

But say that warning 4995, for this compiler (whichever it is), is about using
a deprecated function: then that is the problem, and you can ignore the
proposed solution above and concentrate on the problem.

E.g., you can put the usage of a deprecated function in its own module (header
plus implementation) , by wrapping it in a similar function, and put the
#pragma -- which in general should be enclosed in #ifdef...#endif compiler
version checking -- at the top of the implementation file.

This is a much better solution because it abstracts the use of the deprecated
function.

Which not only can make the client code more clear, but means you only have to
change _one_ place when that deprecated function is actually removed.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 6 '05 #8
In article <43************ **@sintef.no>,
Ben Hetland <be***********@ sintef.no> wrote:
David Resnick wrote:
#if defined(DO_DIRT YWORK)
#include "dirtywork. h"
#endif

Yes, that might work, but means 3 lines of code for every single of
these places that I need this thing.


Just

#include "dirtywork. h"

and it will have within it the DO_DIRTYWORK conditional test
and the dirty code.
--
Chocolate is "more than a food but less than a drug" -- RJ Huxtable
Oct 6 '05 #9
> Ben Hetlandwrote:
The point was of course to use that DO_DIRTYWORK several places
around
the sources.


Why not having dirtywork in a separated function and then call it
whenever you need to?
Sent via Archivaty.com
Oct 6 '05 #10

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

Similar topics

37
2818
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any thoughts about that? "Movement" seems quite an exaggeration. Maybe 2-3 people made some experiments, but nobody within the core Python developers seems to be willing to advocate the introduction of macros. > Why should you care whether the...
8
7237
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to their use as 'functions'; I realise the loss of type-safety and the possible evaluation errors that can occur. However, what would be the harm with numeric and text literals? Consider a number that plays a significant role in the implementation of...
37
2907
by: seberino | last post by:
I've been reading the beloved Paul Graham's "Hackers and Painters". He claims he developed a web app at light speed using Lisp and lots of macros. It got me curious if Lisp is inherently faster to develop complex apps in. It would seem if you could create your own language in Lisp using macros that that would be quite an advantage.... I realize that Python has operator overloading and OOP so I'm not sure.
12
3063
by: Steven T. Hatton | last post by:
I've noticed in different places where people who write #MACROS use a null do/while. For example: do { POINTER = new CONSTRUCTOR; \ if (POINTER == 0) { errno = ENOMEM; ACE_THROW_INT (EXCEPTION); } \ } while (0) What does that accomplish? --
6
1604
by: David Pokorny | last post by:
Hi, Just wondering if anyone has considered macros for Python. I have one good use case. In "R", the statistical programming language, you can multiply matrices with A %*% B (A*B corresponds to pointwise multiplication). In Python, I have to type import Numeric matrixmultiply(A,B)
3
2667
by: Stephen Sprunk | last post by:
On a project I'm working on, I ran across the following macros: /* assume s is struct stream *, s->p is char, v is unit16_t or uint32_t */ #define in_uint16_le(s,v) { v = *((s)->p++); v += *((s)->p++) << 8; } #define in_uint32_le(s,v) { in_uint16_le(s,v) \ v += *((s)->p++) << 16; v += *((s)->p++) << 24; } I'm personally not fond of function-like macros and wanted to turn these into static inline functions, but I'm having trouble doing...
9
387
by: Ben Hetland | last post by:
....in certain cituations they can be useful if not for anything else, then at least for saving a lot of repetetetetetitititive typing. :-) Beyond the point of "do something better instead", I'm curious about how the following syntactical problem can be solved. It should apply equally to C and C++ as it mainly is a preprocessor-related problem. I tryed to define something similar to the following example: ...
4
1700
by: Chronologic | last post by:
All, I have an issue I would like some expert help on. I understand, or so I believe, that C# does not support the concept of a "compile time macro". At least not in the sense I'm looking for. While many users contend that macros are inherently evil, I would argue that no - they are not, they have a function. That function is sometimes -- perhaps much too often -- abused, but they do have a
5
1860
by: Alexander Adam | last post by:
Hi! I've got an issue. I have more or less an internal class model for which I want to write various bindings e.g. for Windows COM. Now I've figured to make my internal classes based on IDispatchEx which works fine. The biggest pain I have right now is that I need to register properties and methods dynamically and I need to call those dynamically, too. Take the following example: struct method {
0
9704
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
9571
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
10318
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...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
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...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3803
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.