Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In the following code:
C++
void f()
{
    int a=2, b=3;
    M(a+1);
    M(swap(a,b));
}

I'm looking for such an output:
a+1 --> 3
swap(a,b) --> x
Assume swap is a function which swaps the values of a and b and returns void.
How to write the M macro?
In another words, I need the result if void, it doesn't output anything other than x in the second column and if it's not void it shows the result.

What I have tried:

template functions and variadic macros.
Posted
Updated 14-Oct-17 20:34pm
Comments
Richard MacCutchan 8-Oct-17 13:06pm    
Your function does not take any parameters, and does not return a result, so it really serves no purpose. Furthermore your question is impossible to understand.
ilostmyid2 8-Oct-17 13:15pm    
1. many programs are written with void main(void) which do many things!
2. the question is clear. i described in glimpse.
PIEBALDconsult 8-Oct-17 15:00pm    
2. No, it is not clear. Please use "Improve question" to add detail and context.
Richard MacCutchan 8-Oct-17 15:47pm    
1. That is true, but they do not, and cannot return a result.
2. "In another words, I need the result if void, it doesn't output anything other than x in the second column and if it's not void it shows the result.". Sorry but I have no idea what that is supposed to mean.
PIEBALDconsult 8-Oct-17 13:28pm    
Ah, so the M macro is supposed to output the value of its argument? So, something along the lines of
# define M(x) printf ( x )
And that works for the argument a+1 , but not for the argument swap(a,b) , do I understand that right?

Quote:
how to treat void as argument?

My short answer: you don't!
Basically, in C/C++ language, 'void' means no value.
Quote:
Assume swap is a function which swaps the values of a and b and returns void.

In your situation, 'void' is used to say that a piece of code do not return any value.
Quote:
1. many programs are written with void main(void) which do many things!

First void means that main do not return any value to caller, and the caller is the OS and it don't care about a return value.
Second void means that main do not receive any value from caller.
Void type - Wikipedia[^]
 
Share this answer
 
v2
class variant
{
	char d_var[80];

public:
	variant() { d_var[0] = 0; }
	variant(int a) { sprintf(d_var, "%d", a); }
	variant(LPCSTR var) { strcpy(d_var, var); }

	operator LPCSTR() const { return d_var; }
	//string operator ,(const string &arg) const { return d_var; }
};

string operator ,(const variant &var, const string &arg) { return var; }

void void_func() {}

struct PrintHelper
{
	operator string() const { return "void"; }
} printHelper;

void print(LPCSTR title, const string &result)
{
	printf("expression \"%s\" evaluates to \"%s\"\n", title, result.c_str());
}

#define M(expr) print(#expr, (expr, printHelper))

void f9()
{
	int a=2, b=3;
	M(10);
	M(a+1);
	M(void_func());
	M(swap(a,b));
}

It outputs to:
expression "10" evaluates to "10"
expression "a+1" evaluates to "3"
expression "void_func()" evaluates to "void"
expression "swap(a,b)" evaluates to "void"

Now I could print an expression as well as the value it evaluates to. It supports void values and doesn't differ whether a value is evaluated to void or not.
Now I wonder in the code, why operator , doesn't work when it's defined as a class variant member function. It apparently shouldn't differ from when is defined as a self-determining operator outside of the class.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900