C++ Logo

std-discussion

Advanced search

¦^Î`: Are new and delete allowed to be defined as macros in C++17?

From: F. v.S. <de34_at_[hidden]>
Date: Sat, 13 Sep 2025 18:18:36 +0000
No, they aren't.

Defining them as macros makes the program have undefined behavior (it seems more correct to say "ill-formed, no diagnostic required" though) in C++17. Since C++26, doing so certainly makes the program ill-formed.

IIUC, MSVC STL permits defining new (but not delete) as a macro as an extension. Such extension is made non-conforming since C++26.

Thanks,
F.v.S.
________________________________
From: Std-Discussion <std-discussion-bounces_at_[hidden]> on behalf of Julian Waters via Std-Discussion <std-discussion_at_[hidden]>
Sent: Sunday, September 14, 2025 0:39
To: std-discussion_at_lists.isocpp.org <std-discussion_at_[hidden]>
Cc: Julian Waters <tanksherman27_at_[hidden]>
Subject: [std-discussion] Are new and delete allowed to be defined as macros in C++17?

Hi all,

In C++17, new and delete are listed under preprocessing-op-or-punc,
meaning unlike other keywords, it seems that the Standard mandates
that the preprocessor consider these character sequences as
operators/punctuators rather than preprocessor identifiers. Under this
assumption, since the first preprocessing token that appears after
#define and #undef must be an identifier and not an operator or
punctuator, this implies that new and delete cannot be defined as
macros nor undefined via #undef, yet all the big 3 C++ compilers still
seem to allow new or delete to be defined and undefined via the
corresponding directives. Is there wording in the Standard somewhere
that clarifies this or are the big 3 compilers non conforming to the
Standard?

Apologies if this is not the right list to discuss this issue. I could
not find any other list that was meant for discussion about the C++
Standard besides this one.

best regards,
Julian
--
Std-Discussion mailing list
Std-Discussion_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2025-09-13 18:18:41