Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the macro func and function func can be distinguished without any ambiguity?

Tags:

c++

#include<iostream>
int func(int, int);
#define func(x,y) x/y+x
int main()
{
    int i, j;

    scanf("%d", &i);
    scanf("%d", &j);

    printf("%d ", func(i + j, 3));
    printf("%d\n", func(i + j, 3));

}

int func(int x, int y) {
    return y / x + y;
}

What should be added to this code so that the 1st output is the result of the macro and the 2nd one is the result of the function?

like image 532
Satyaki Majumder Avatar asked Dec 11 '22 05:12

Satyaki Majumder


2 Answers

Several ways:

printf("%d ", func(i + j, 3)); // Macro call
#undef func // Macro is no longer defined
printf("%d\n", func(i + j, 3)); // Function call

or

printf("%d ", func(i + j, 3)); // Macro call
printf("%d\n", (func)(i + j, 3)); // Function call
like image 58
Jarod42 Avatar answered May 23 '23 07:05

Jarod42


Yet another way:

#define EMPTY

printf("%d\n", func EMPTY (i + j, 3));

Seen in Boost.PP, the EMPTY macro "burns up" the expansion iteration and the resulting func(...) is left untouched.

like image 37
Quentin Avatar answered May 23 '23 06:05

Quentin