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

  • SEARCH
  • Home
  • 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 98911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:17:12+00:00 2026-05-11T00:17:12+00:00

UPDATE: Obviously, you’d want to do this using templates or a base class rather

  • 0

UPDATE: Obviously, you’d want to do this using templates or a base class rather than macros. Unfortunately for various reasons I can’t use templates, or a base class.


At the moment I am using a macro to define a bunch of fields and methods on various classes, like this:

class Example {   // Use FIELDS_AND_METHODS macro to define some methods and fields   FIELDS_AND_METHODS(Example) }; 

FIELDS_AND_METHODS is a multi-line macro that uses stringizing and token-pasting operators.

I would like to replace this with the following kind of thing

class Example {   // Include FieldsNMethods.h, with TYPE_NAME preprocessor symbol   // defined, to achieve the same result as the macro.   #define TYPE_NAME Example   #include 'FieldsNMethods.h' }; 

Here I #define the name of the class (previously the parameter to the macro), and the FieldsNMethods.h file contains the content of the original macro. However, because I’m #including I can step into the code at runtime, when debugging.

However I am having trouble ‘stringizing’ and ‘token pasting’ the TYPE_NAME preprocessor symbol in the FieldsNMethods.h file.

For example, I want to define the destructor of the class in FieldsNMethods.h, so this would need to use the value of TYPE_NAME as below:

~TYPE_NAME() {   //... } 

But with TYPE_NAME replaced by its value.

Is what I’m attempting possible? I can’t use the stringizing and token-pasting operators directly, because I’m not in a macro definition.

  • 1 1 Answer
  • 0 Views
  • 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. 2026-05-11T00:17:13+00:00Added an answer on May 11, 2026 at 12:17 am

    This cries out for a template.

    class Example<class T> {     ...class definition... }; 

    The direct answer to the last part of your question – ‘given that I’m not in a macro definition any more, how do I get pasting and stringizing operators to work’ – is ‘You can’t’. Those operators only work in macros, so you’d have to write macro invocations in order to get them to work.

    Added:

    @mackenir said ‘templates are not an option’. Why are templates not an option? The code is simulating templates the old-fashioned pre-standard, pre-template way, and does so causing much pain and grief. Using templates would avoid that pain — though there’d be a conversion operation.

    @mackenir asked ‘is there a way to make things work with macros?’ Yes, you can, but you should use templates – they are more reliable and maintainable. To make it work with macros, then you’d have to have the function names in the code in the included header be macro invocations. You need to go through a level of indirection to get this to work correctly:

    #define PASTE_NAME(x, y) PASTE_TOKENS(x, y) #define PASTE_TOKENS(x, y) x ## y  #define TYPE_NAME Example int PASTE_NAME(TYPE_NAME, _function_suffix)(void) { ... } 

    This level of indirection is an often necessary idiom for both tokenizing and stringizing operators.


    Additional comments from @mackenir indicate continued problems. Let’s make it concrete.

    At the moment I am using a macro to define a bunch of fields and methods on various classes, like this:

    class Example {     // Use FIELDS_AND_METHODS macro to define some methods and fields     FIELDS_AND_METHODS(Example) }; 

    FIELDS_AND_METHODS is a multi-line macro that uses stringizing and token-pasting operators.

    I would like to replace this with the following kind of thing

    class Example {     // Include FieldsNMethods.h, with TYPE_NAME preprocessor symbol     // defined, to achieve the same result as the macro.     #define TYPE_NAME Example     #include 'FieldsNMethods.h' }; 

    OK. To make this concrete, we need a FIELDS_AND_METHODS(type) macro that is multi-line and uses token-pasting (I’m not going to deal with stringizing – the same basic mechanisms will apply, though).

    #define FIELDS_AND_METHODS(type) \     type *next; \     type() : next(0) { } \     type * type ## _next() { return next; } 

    With luck, this declares a member of the type ‘pointer to argument type’, a constructor for that type, and a method (Example_next in this case) that returns that pointer.

    So, this might be the macro – and we need to replace it such that the ‘#include’ does the equivalent job.

    The content of fieldsNmethods.h becomes:

    #ifndef TYPE_NAME #error TYPE_NAME not defined #endif #define FNM_PASTE_NAME(x, y)    FNM_PASTE_TOKENS(x, y) #define FNM_PASTE_TOKENS(x, y)  x ## y  TYPE_NAME *next; TYPE_NAME() : next(0) { } TYPE_NAME * FNM_PASTE_NAME(TYPE_NAME, _next)() { return next; }  #undef FNM_PASTE_NAME #undef FNM_PASTE_TOKENS 

    Note that the header would not contain multiple-inclusion guards; its raison d’etre is to allow it to be included multiple times. It also undefines its helper macros to permit multiple inclusion (well, since the redefinitions would be identical, they’re ‘benign’ and wouldn’t cause an error), and I prefixed them with FNM_ as a primitive namespace control on the macros. This generates the code I’d expect from the C pre-processor. and G++ doesn’t witter but produces an empty object file (because the types declared are not used in my example code).

    Note that this does not require any changes to the calling code except the one outlined in the question. I think the question should be improved using the SPOT ‘Single Point of Truth’ principle (or DRY ‘Don’t Repeat Yourself’):

    #define TYPE_NAME Example class TYPE_NAME {     // Include FieldsNMethods.h, with TYPE_NAME preprocessor symbol     // defined, to achieve the same result as the macro.     #include 'FieldsNMethods.h' }; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 161k
  • Answers 161k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That probably was me :-) By default, your service and… May 12, 2026 at 11:49 am
  • Editorial Team
    Editorial Team added an answer The moreUnit plugin probably works for you. Capabilities (from its… May 12, 2026 at 11:49 am
  • Editorial Team
    Editorial Team added an answer Let’s assume we already have the key and value in… May 12, 2026 at 11:49 am

Related Questions

UPDATE: Obviously, you'd want to do this using templates or a base class rather
Stupid question time - how do you update the title attribute of a control?
When doing a cvs update , you get a nice summary of the state
What's the general consensus? If you need to change the database after a given
How can you go about updating a record without having to select the data

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.