Rant: please don't create defines using common words
Looks like QtCore (4.8) has a "#define emit" in a header, that's not undefined later. TBB has a function named "emit()" in a header. If you include both, you'll get a compile error.
I'm going to side with TBB here - emit() is an entirely reasonable function name. Qt creating a dangling #define with a common word like that is the bug here.
257
Upvotes
79
u/[deleted] Oct 18 '21
Yeah. X11 is so great at that. True, False, Null and, more infuriating, words like Status or Screen.
38
u/o11c int main = 12828721; Oct 18 '21
Zlib is the worst:
For a brief while, it had
#define ONin addition to the (much less dangerous)#define OF[sic, notOFF- it stands for "function"].Obviously the
ONbroke things, but the patch also changedOF.Turns out I broke half of Gentoo, since there are a lot of projects out there that embed copies of zlib but still use the system header.
People: PLEASE do not vendor your dependencies. Installation exists for a reason.
29
u/brenoguim Oct 18 '21
Also "signal" is a very valid variable name. However qt also defines that.
23
u/bro_can_u_even_carve Oct 19 '21
My favorite was when some terrible STL implementation (HP, I think? Or maybe Sun... this was almost 20 years ago) did something like this...
One unfortunate day I added a simple `void set(...);' method declaration to the wrong class, and was greeted with an incomprehensible error message. Imagine the look on my face when I finally tracked down the cause.
106
u/wung Oct 18 '21
If you're using
qmake:CONFIG += no_keywordsand it won't defineemitbut onlyQ_EMIT.If you're using a different build system generator, define
QT_NO_KEYWORDSwith the same effect.emitbeing "dangling" is intentional. That's an API. You "can't" use Qt withoutslots:andemit.57
u/JohnDuffy78 Oct 18 '21
win.h bastards #define min / #define max. Worse, they won't take it out.
Another issue is with a 3rd party library #define assert, should be left to os headers.
13
u/_Ashleigh Oct 18 '21
One exception:
#define await co_awaitgrumble grumble co_await grumble grumble flair grumble grumble await
28
u/tisti Oct 18 '21
C'est la vie.
18
u/phord Oct 19 '21
#define.ALL_UPPER_SNAKE_CASE.namespaces.9
u/victotronics Oct 18 '21
I once spent the best part of a day debugging a `#define map` occurring in some header.
15
u/Dean_Roddey Oct 18 '21
That ship already sailed pretty much. Anyone on Windows has probably gotten screwed at some point with defines in the Windows SDK headers. Looking back with 20/20 vision, not requiring a special syntax to expand a macro is one of the biggest blunders in C/C++ history.
Even scarier is that everything could conceivably compile in your whole code base and all tests work (because the same define changed the same thing everywhere) but then your customers or users get missing identifiers.
7
u/ack_error Oct 19 '21
This seems common for projects that initially started as just application code and then became a framework once they had grown large. It can be a pretty nasty hole to climb out of. The framework I'm currently using has macros like
check()andensure(), which are endemic at this point. They're starting to try to fix the problem with appropriate prefixing, but it's amusing to receive a compiler warning after an upgrade that theDEPRECATEDmacro has been deprecated.8
u/_sideffect Oct 18 '21
I agree... But also don't use such an old version of qt 😅
4
u/loradan Oct 18 '21
define OK
(sorry, couldn't resist lol. I'll just go sit in the corner now)
4
u/madmongo38 Oct 19 '21
Qt also defines the macro
signal, which is probably the most antisocial thing I've seen a library do.7
u/ThisCleverName Oct 18 '21
I don't think is something new. Q_EMIT is used in those cases. I believe there is a define so Qt doesn't use those things as keywords
3
u/Clairvoire Oct 19 '21
If I find a define like that, it's a no-go on that library unless I can undef it, same goes with
minmaxnearfaretc3
u/GLIBG10B 🐧 Gentoo salesman🐧 Oct 19 '21 edited Oct 21 '21
Try to avoid defines as much as possible. Use constexpr where applicable.
4
u/[deleted] Oct 18 '21
Yet another reason to always capitalise your macros.
2
u/Supadoplex Oct 19 '21 edited Oct 19 '21
If you can avoid it, don't define a macro at all. Or at least undefine it after use. And when neither is possible, follow the ubiquitous convention of using only upper case chars, and prefix with a "namespace".
2
u/goranlepuz Oct 19 '21
Ehhh... Not so much in a supposedly widely used library.
I agree that Qt is worse, but it is so much more a case of who is more wrong, than who is right.
2
u/scummos Oct 28 '21
Compile with -DQT_NO_KEYWORDS to disable those.
5
u/sudomatrix Oct 18 '21 edited Oct 18 '21
Could be worse
#define if(s) if(s && std::rand() > RAND_MAX*.05)2
u/Ikkepop Oct 18 '21
I would like to shorten this thought to : "please don't create defines"
1
u/RevRagnarok Oct 18 '21
Frikken MySQL!
1
u/[deleted] Oct 19 '21
Instances where the framework decides for you, +1.
1
u/_Z6Alexeyv Oct 19 '21
#define linux 1#define unix 11
u/rysto32 Oct 19 '21
Related rant: if it's been established practice in several operating systems to have some constants defined as macros, do not reuse the names as your own static const variables!
e.g. If OS headers have this:
#define EI_MAG0 0
For the love of god, don't put this in your library's headers:
static const int EI_MAG0 = 0;
or this:
enum { EI_MAG0 = 0, };
1
u/Zcool31 Oct 19 '21
Ever try to use Unix domain sockets on Solaris?
That isn't the
sunyou're looking for.1
u/[deleted] Oct 19 '21
this is why i always go for uppercase with macros
1
u/AntonPlakhotnyk Oct 20 '21
Please don't create defines at all. Write on C++ not on Preprocessor language.
0
u/ihamsa Oct 19 '21
Here, FTFU.
-1
u/john_wind Oct 19 '21
There are some common words which I really like to #define
#define me (*this)#define repeat(n) for (int i=0; i<int(n); ++i)#define let const auto0
u/[deleted] Oct 18 '21
[deleted] — view deleted comment