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 8346187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:55:42+00:00 2026-06-09T06:55:42+00:00

I want to create a define to parse function signature and using Boost Preprocessor

  • 0

I want to create a define to parse function signature and using Boost Preprocessor create something like this:

MY_DEFINE std::string fun(int t, float b)
{

or at least:

MY_DEFINE(std::string)(fun)(int t, float b)
{

that would generate:

class fun_in
{
    int t;
    float b;
}

class fun_out
{
    std::string value;
}

void my_fun_wrapper(int t, float b)
{
}

std::string fun(int t, float b)
{
    my_fun_wrapper(t, b);

for each function with that define.

Is it possible to create such define wrapper for function of N incoming arguments and any return type via Boost Preprocessor?

  • 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. Editorial Team
    Editorial Team
    2026-06-09T06:55:44+00:00Added an answer on June 9, 2026 at 6:55 am

    Well, the preprocessor can’t parse tokens without pre-telling it. So you will need to use a lot more parenthesis instead. Here’s what it would look like:

    DEFINE( (std::string)(fun)((int) a, (float) b) )
    {
        return "Hello World!";
    }
    

    Here’s how to create the macro using boost(I assume you are familiar with its preprocessor library). First is to define some macros for handling parenthesis, because boost doesn’t handle sequences with commas at all:

    #define REM(...) __VA_ARGS__
    #define EAT(...)
    
    // Retrieve the type
    #define TYPEOF(x) DETAIL_TYPEOF(DETAIL_TYPEOF_PROBE x,)
    #define DETAIL_TYPEOF(...) DETAIL_TYPEOF_HEAD(__VA_ARGS__)
    #define DETAIL_TYPEOF_HEAD(x, ...) REM x
    #define DETAIL_TYPEOF_PROBE(...) (__VA_ARGS__),
    // Strip off the type
    #define STRIP(x) EAT x
    // Show the type without parenthesis
    #define PAIR(x) REM x
    

    Next, you need to handle the args in three different ways. First, is to output them as member variable(like int a; float b;). Then as functions arguments(like (int a, float b)). Then finally as forward arguments to pass along to the other function(like (a, b)).

    #define DETAIL_DEFINE_MEMBERS_EACH(r, data, x) PAIR(x);
    #define DETAIL_DEFINE_ARGS_EACH(r, data, i, x) BOOST_PP_COMMA_IF(i) PAIR(x)
    #define DETAIL_DEFINE_FORWARD_EACH(r, data, i, x) BOOST_PP_COMMA_IF(i) STRIP(x)
    
    #define DETAIL_DEFINE_MEMBERS(args) BOOST_PP_SEQ_FOR_EACH(DETAIL_DEFINE_MEMBERS_EACH, data, BOOST_PP_VARIADIC_TO_SEQ args)
    #define DETAIL_DEFINE_ARGS(args) BOOST_PP_SEQ_FOR_EACH_I(DETAIL_DEFINE_ARGS_EACH, data, BOOST_PP_VARIADIC_TO_SEQ args)
    #define DETAIL_DEFINE_FORWARD(args) BOOST_PP_SEQ_FOR_EACH_I(DETAIL_DEFINE_FORWARD_EACH, data, BOOST_PP_VARIADIC_TO_SEQ args)
    

    Next we create a DETAIL_DEFINE macro that take three parameters. The first is the name of the function, the arguments, and then the return value. This will produce the classes and functions, like you want:

    #define DETAIL_DEFINE(name, args, ...) \
    struct BOOST_PP_CAT(name, _in) \
    { \
        DETAIL_DEFINE_MEMBERS(args) \
    }; \
    struct BOOST_PP_CAT(name, _out) \
    { \
        __VA_ARGS__ value; \
    }; \
    __VA_ARGS__ BOOST_PP_CAT(name, _impl) DETAIL_DEFINE_ARGS(args) ; \
    __VA_ARGS__ name DETAIL_DEFINE_ARGS(args) \
    { \
        return BOOST_PP_CAT(name, _impl) DETAIL_DEFINE_FORWARD(args); \
    } \
    __VA_ARGS__ BOOST_PP_CAT(name, _impl) DETAIL_DEFINE_ARGS(args)
    

    Finally, the DEFINE macro will parse out all the parenthesis and pass them to the DETAIL_DEFINE macro:

    #define DEFINE(x) DETAIL_DEFINE(TYPEOF(STRIP(x)), (TYPEOF(STRIP(STRIP(x)))), TYPEOF(x))
    

    So now when you write:

    DEFINE( (std::string)(fun)((int) a, (float) b) )
    {
        return "Hello World!";
    }
    

    It should output:

    struct fun_in
    {
        int a;
        float b;
    };
    struct fun_out
    {
        std::string value;
    };
    std::string fun_impl(int a, float b);
    std::string fun(int a, float b)
    {
        return fun_impl(a, b);
    }
    std::string fun_impl(int a, float b)
    {
        return "Hello World!";
    }
    

    Note that this won’t work in MSVC, there are workarounds though. Also, you need to compile with the -DBOOST_PP_VARIADICS=1.

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

Sidebar

Related Questions

This the model that I want to create using json file Ext.define('Users', { extend:
I want to define the block as a string, then create the lambda. The
i have a xml like this I want to parse the xml, build a
I want to create an macro for trimming my string. I use this code
I want to create my own custom collection type. I define my collection as:
when i create a thread, i want to pass several arguments. So i define
I want to create a function which receives a single argument that holds the
I am using ASP.NET MVC3 with Razor and would like to define a route
I want to create an object that can parse a certain filetype. I've looked
i am using this widget to get my users to create accounts: <iframe src=http://www.facebook.com/plugins/registration.php?

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.