Viktor Sehr
Viktor Sehr

Reputation: 13099

Macros with multiple parentheses

How can I create a macro with multiple parentheses? I don't need variadic number of arguments, I just want to be able to call my macro like.

MY_MACRO(arg0, arg1)(arg2) 

...instead of:

MY_MACRO(arg0, arg1, arg2) 

Update:

Let's say I have a macro defined like; #define MY_MACRO(a, b, c) (a*b/c) and called like MY_MACRO(1,2,3). How do I transform this macro to being called like MY_MACRO(1, 2)(3)

I.e., what I want to do is write my macro as usual, only take the last argument in it's own parenthesis.

Upvotes: 2

Views: 1031

Answers (2)

user2100815
user2100815

Reputation:

#include <iostream>
using namespace std;

#define MY_MACRO(a, b) (a * b)/ MY_MACRO1
#define MY_MACRO1(c) c

int main() {
    int n = MY_MACRO( 5,6)(3);
    cout << n << endl;
}

Upvotes: 1

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507343

#define MY_MACRO(a, b) a, b, MY_MACRO1
#define MY_MACRO1(c) c

Now doing

MY_MACRO(arg0, arg1)(arg2)

Will do

MY_MACRO(arg0, arg1)(arg2)
a, b, MY_MACRO1 (arg2)
arg0, arg1, MY_MACRO1 (arg2)
arg0, arg1, c
arg0, arg1, arg2

Upvotes: 4

Related Questions