Discussion:
Use of feature test macros
Add Reply
Roland Illig
2024-11-02 21:36:10 UTC
Reply
Permalink
#if (_POSIX_C_SOURCE - 0 >= 200809L) || defined(_NETBSD_SOURCE)
Are the parentheses around the first condition really necessary? If so,
for which cases?

Is the "- 0" in the first expression really necessary? If so, for which
cases?
#if _POSIX_C_SOURCE >= 200809L || defined(_NETBSD_SOURCE)
Roland


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Robert Elz
2024-11-02 23:52:38 UTC
Reply
Permalink
Date: Sat, 2 Nov 2024 22:36:10 +0100
From: Roland Illig <***@gmx.de>
Message-ID: <852461a5-c852-40d0-a810-***@gmx.de>

| In the system headers, we use this pattern a lot:
| > #if (_POSIX_C_SOURCE - 0 >= 200809L) || defined(_NETBSD_SOURCE)
|
| Are the parentheses around the first condition really necessary? If so,
| for which cases?

Do they hurt? Parens generally make it easier to read exprs like
this, within reason, almost never harder.

| Is the "- 0" in the first expression really necessary? If so, for which
| cases?

When _POSIX_C_SOURCE is undefined (or defined to nothing).
That is the standard idiom for testing all of these feature macros.

kre


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Roland Illig
2024-11-03 01:02:59 UTC
Reply
Permalink
Post by Robert Elz
Date: Sat, 2 Nov 2024 22:36:10 +0100
| Is the "- 0" in the first expression really necessary? If so, for which
| cases?
When _POSIX_C_SOURCE is undefined (or defined to nothing).
That is the standard idiom for testing all of these feature macros.
When _POSIX_C_SOURCE (or any other identifier) is undefined, the
preprocessor replaces it with 0. This has been spelled out in the C
standards since C90, for example in C23 6.10.2p13, and I thought it
would be common knowledge, that's why I didn't mention it in my original
questions.

From what I'm reading out of <sys/featuretest.h>, _POSIX_C_SOURCE is not
defined to empty for any version of POSIX, thus making the "- 0" part
superfluous. That part may be necessary for _XOPEN_SOURCE, but not for
_POSIX_C_SOURCE.

Roland


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Mouse
2024-11-02 23:57:04 UTC
Reply
Permalink
#if (_POSIX_C_SOURCE - 0 >= 200809L) || defined(_NETBSD_SOURCE)
Are the parentheses around the first condition really necessary? If
so, for which cases?
Probably not...I *think*.
Is the "- 0" in the first expression really necessary? If so, for
which cases?
The case where _POSIX_C_SOURCE is not defined at all. (Whether that
case is worth bothering with, that's arguable. But I think that's
probably the point of the -0.)

This is a valid #if:

#if ( - 0 > 200809L) || defined(_NETBSD_SOURCE)

This is not:

#if ( > 200809L) || defined(_NETBSD_SOURCE)

Of course, if _POSIX_C_SOURCE is defined as

do } case ~ ( ) j -

that won't help. But in that sort of case nothing will.

/~\ The ASCII Mouse
\ / Ribbon Campaign
X Against HTML ***@rodents-montreal.org
/ \ Email! 7D C8 61 52 5D E7 2D 39 4E F1 31 3E E8 B3 27 4B

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Robert Elz
2024-11-03 00:31:18 UTC
Reply
Permalink
Date: Sat, 2 Nov 2024 19:57:04 -0400 (EDT)
From: Mouse <***@Rodents-Montreal.ORG>
Message-ID: <***@Stone.Rodents-Montreal.ORG>


| The case where _POSIX_C_SOURCE is not defined at all. (Whether that
| case is worth bothering with, that's arguable. But I think that's
| probably the point of the -0.)

It is, and it is actually the common case, essentially none of
NetBSD builds with _POSIX_C_SOURCE or _XOPEN_SOURCE or ...
defined (just _NETBSD_SOURCE).

So yes, it is worth bothering with (and FWIW for some of these, though
not I believe _POSIX_C_SOURCE back in the past they were just defined,
or not defined, only more recently (like the past couple of decades)
did values start to be assigned).

kre


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Valery Ushakov
2024-11-03 10:17:04 UTC
Reply
Permalink
Post by Roland Illig
Post by Robert Elz
Date: Sat, 2 Nov 2024 22:36:10 +0100
| Is the "- 0" in the first expression really necessary? If so, for which
| cases?
When _POSIX_C_SOURCE is undefined (or defined to nothing).
That is the standard idiom for testing all of these feature macros.
When _POSIX_C_SOURCE (or any other identifier) is undefined, the
preprocessor replaces it with 0. This has been spelled out in the C
standards since C90, for example in C23 6.10.2p13, and I thought it
would be common knowledge, that's why I didn't mention it in my original
questions.
The idiom (though I learned it as FOO + 0, not minus) is intended to
protect you from contexts where you have in scope plain "#define FOO"
with an empty replacement list (NB: -DFOO on the command line is
different as it means -DFOO=1). In that case "after all replacements
due to macro expansion" the FOO is no longer there and is not one of
"remaining identifiers" that "are replaced with the pp-number 0".

Now, we can argue whether _POSIX_C_SOURCE can be defined that way, but
that's how idioms are, you use them as a canned recipe so to say, so I
don't think that would be a tangential discussion at best.

-uwe

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
David Holland
2024-11-08 17:01:08 UTC
Reply
Permalink
Post by Roland Illig
#if (_POSIX_C_SOURCE - 0 >= 200809L) || defined(_NETBSD_SOURCE)
Are the parentheses around the first condition really necessary? If so,
for which cases?
Is the "- 0" in the first expression really necessary? If so, for which
cases?
I was just discussing this idiom a few days ago, in the context of
program checkers that warn when you do #if SYM when it isn't defined
(which is legal and well defined, but also often a mistake) and one of
the virtues of this idiom is that such checkers can recognize it and
shut up.

For that purpose it's better to keep the parentheses as well as the
arithmetic.
--
David A. Holland
***@netbsd.org

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Loading...