Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 841313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:46:25+00:00 2026-05-15T05:46:25+00:00

Consider the following macro definitions and invocation: #define x x[0] #define y(arg) arg y(x)

  • 0

Consider the following macro definitions and invocation:

#define x x[0]
#define y(arg) arg

y(x)

This invocation expands to x[0] (tested on Visual C++ 2010, g++ 4.1, mcpp 2.7.2, and Wave).

Why? Specifically, why does it not expand to x[0][0]?

During macro replacement,

A parameter in the replacement list…is replaced by the corresponding argument after all macros contained therein have been expanded. Before being substituted, each argument’s preprocessing tokens are completely macro replaced (C++03 §16.3.1/1).

Evaluating the macro invocation, we take the following steps:

  • The function-like macro y is invoked with x as the argument for its arg parameter
  • The x in the argument is macro-replaced to become x[0]
  • The arg in the replacement list is replaced by the macro-replaced value of the argument, x[0]

The replacement list after substitution of all the parameters is x[0].

After all parameters in the replacement list have been substituted, the resulting preprocessing token sequence is rescanned…for more macro names to replace (C++03 §16.3.4/1).

If the name of the macro being replaced is found during this scan of the replacement list…it is not replaced. Further, if any nested replacements encounter the name of the macro being replaced, it is not replaced (C++03 §16.3.4/2).

The replacement list x[0] is rescanned (note that the name of the macro being replaced is y):

  • x is identified as an object-like macro invocation
  • x is replaced by x[0]

Replacement stops at this point because of the rule in §16.3.4/2 preventing recursion. The replacement list after rescanning is x[0][0].

I have clearly misinterpreted something since all of the preprocessors I’ve tested say I am wrong. In addition, this example is a piece of a larger example in the C++0x FCD (at §16.3.5/5) and it too says that the expected replacement is x[0].

Why is x not replaced during rescanning?

C99 and C++0x effectively have the same wording as C++03 in the quoted sections.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:46:26+00:00Added an answer on May 15, 2026 at 5:46 am

    I believe you’ve quoted the crucial paragraph, you just stopped too soon. 16.3.4/2 (emphasis mine):

    If the name of the macro being replaced is found during this scan of
    the replacement list (not including
    the rest of the source file’s
    preprocessing tokens), it is not
    replaced. Further, if any nested
    replacements encounter
    the name of the macro being replaced,
    it is not replaced. These nonreplaced
    macro name preprocessing tokens are no
    longer available for further
    replacement
    even if they are later
    (re)examined in contexts in which that
    macro name preprocessing token would
    otherwise have been replaced.

    So, when x is replaced with x[0] during the parameter substitution of y, it is fully macro replaced, meaning that it is rescanned at that point, and x is caught by the recursion rule. This means that the x in x[0] is no longer eligible for further replacement, including during the rescanning of the partly-expanded result of y(x).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following setup: A windows PC with a LAN interface and a WiFi
Consider the following ruby code test.rb: begin puts thisFunctionDoesNotExist x = 1+1 rescue Exception
Consider the following 2 queries: select tblA.a,tblA.b,tblA.c,tblA.d from tblA where tblA.a not in (select
Consider the following method signatures: public fooMethod (Foo[] foos) { /*...*/ } and public
Consider the following code: void Handler(object o, EventArgs e) { // I swear o
Consider the following SQL: BEGIN TRAN SET TRANSACTION ISOLATION LEVEL READ COMMITTED INSERT Bands
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.