473,835 Members | 2,159 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
11 2865
Alf P. Steinbach wrote:
Your abstraction of your proposed _solution_ of the problem is only
preprocessor-related, but whether problem is, is another question.
That is true, and for instance for C++ the problem could also be solved
using templates in combination with macros in a single header-file.
However, I posted the question to challenge my shortcoming with the
preprocessor's syntax rules, of which some useful comments have been
posted I think. I would also like to have something that could work in
both languages.

Macros can't generate macros.
This is not about generating macros, but about using other preprocessor
features within a preprocessor feature itself, like a macro definition is.

However, I've seen a similar thing that comes very close to generating
macros many times. That is when macros are built from other macros, and
the building of those macros relies on conditional compilations (another
preprocessor feature) to decide how the macro is actually composed. And
that could happen in multiple levels of "macro nesting". So at least the
preprocessor can 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.
Yes, the original problem was with one of Microsoft's compilers, as you
probably guessed. And deprecated functions are already isolated to a few
files. I used this only for illustration. The specific problem also
included other pragmas and other preprocessor features like #ifdef, but
I thought that wasn't so important in illustrating my question.

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


I definitely agree with much of this, but just would like to comment
that not only functions may be deprecated, but also types for instance
(as can classes in C++). Types often have a tendency to pollute the rest
of the "client code" with its presense far beyond that isolated module.

Also, as happens with functions as well, prototypes usually need to be
included in header files which are again included in the "client code".
Fine, except when a prototype uses a deprecated type, which is when
"pollution" easily occurs.

I guess it is often due to some laziness among us that things like that
happen, because with careful considerations in the first place it must
be possible to avoid that kind of practice. However, it is just a
real-life observation, and it really requires great skill and foresight
to be able to foresee everything that will become deprecated in the
future. Say, which one of us can claim that they will only need to
modify one of their code units completely independently of all other
source code in case of the hypothetical event that (say) 'size_t' would
become deprecated one day?

--
-+-Ben-+-
Oct 7 '05 #11
* Ben Hetland:
Alf P. Steinbach wrote:
Your abstraction of your proposed _solution_ of the problem is only
preprocessor-related, but whether problem is, is another question.
That is true, and for instance for C++ the problem could also be solved
using templates in combination with macros in a single header-file.
However, I posted the question to challenge my shortcoming with the
preprocessor's syntax rules, of which some useful comments have been
posted I think. I would also like to have something that could work in
both languages.


Cross-posted to clc and clc++, I didn't notice.

Cross-language solutions for C++ and C just mean C.

So I think the original question doesn't belong in clc++, but it's a bit late
to fix that now.

Macros can't generate macros.


This is not about generating macros, but about using other preprocessor
features within a preprocessor feature itself, like a macro definition is.


Yes. Actually what I wrote, as opposed to what I meant, is blatantly false...

However, I've seen a similar thing that comes very close to generating
macros many times. That is when macros are built from other macros, and
the building of those macros relies on conditional compilations (another
preprocessor feature) to decide how the macro is actually composed. And
that could happen in multiple levels of "macro nesting". So at least the
preprocessor can generate macros...
For C++, see the amazing "make the preprocessor jump through hoops and give a
rock n' roll concert" library -- or whatever it should be called -- at
<url: http://boost.org/libs/preprocessor/doc/index.html>.

...
Also, as happens with functions as well, prototypes usually need to be
included in header files which are again included in the "client code".
Fine, except when a prototype uses a deprecated type, which is when
"pollution" easily occurs.


For C++, one known solution to that is the PIMPL a.k.a. "compiler firewall"
a.k.a many other things idiom.
<url: http://www.gotw.ca/gotw/024.htm>.
<url: http://www.gotw.ca/gotw/028.htm>.
<url: http://www.gotw.ca/gotw/059.htm> (where the use of std::auto_ptr for an
incomplete type is incorrect; this is the most infamous example of that, the
one often cited when the topicc pops up, still not fixed!).

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

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

Similar topics

37
2821
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
7241
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
2911
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
3065
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
1605
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
2669
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
1702
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
9652
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
10523
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
10560
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
9345
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
7766
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
6966
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
5636
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3088
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.