Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ macro

Status
Not open for further replies.

globos

Programmer
Nov 8, 2000
260
FR
Hi,

I have declared the following macro :

# define old(declaration) (declaration)

and then used it :

void a_proc ()
{
old(int i = 0);
}

But I get the errors :
error C2146: syntax error : missing ')' before identifier 'i'
error C2065: 'i' : undeclared identifier
error C2059: syntax error : ')'

I thought we can put what we want in macros. What is the problem here?

--
Globos
 
>I thought we can put what we want in macros.

In theory yes. But is usually bad practice to do so.

>What is the problem here?

1) You don't really know whay you are defining such a macro ;-)
2)
The macro expands to
((int i = 0);)

which is not a valid c++ syntax.

Use macros with care!

For instance consider a macro like
#define max(a,b) { a>b ? a : b }

and a usage like
max(a++, b--)
how might a and be be affected?





/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Thanks PerFnurt,

my problem is solved, I was confused by the use of macros.
But I know why I am defining such a macro! :)
I want to emulate the 'old' mecanism of Eiffel.
For example consider a basic Counter class:

<pre>
<tt>
<b><font color=#000080>#ifdef</font></b> DBC
<b><font color=#000080># define</font></b> <b><font color=#000000>old</font></b><font color=#990000>(</font>decl<font color=#990000>)</font> decl
<b><font color=#000080># define</font></b> <b><font color=#000000>ensure</font></b><font color=#990000>(</font>expr<font color=#990000>)</font> <b><font color=#000000>assert</font></b> <font color=#990000>(</font>expr<font color=#990000>)</font>
<b><font color=#000080>#else
</font></b>
<b><font color=#000080># define</font></b> <b><font color=#000000>old</font></b><font color=#990000>(</font>decl<font color=#990000>)</font>
<b><font color=#000080># define</font></b> <b><font color=#000000>ensure</font></b><font color=#990000>(</font>expr<font color=#990000>)</font>
<b><font color=#000080>#endif
</font></b>

<b><font color=#0000FF>class</font></b> Counter
<font color=#FF0000>{</font>
<b><font color=#0000FF>public</font></b><font color=#990000>:</font>
<b><font color=#000000>Counter</font></b> <font color=#990000>(</font><font color=#009900>int</font> init<font color=#990000>)</font> <font color=#990000>:</font>
<b><font color=#000000>_counter</font></b> <font color=#990000>(</font>init<font color=#990000>)</font>
<font color=#FF0000>{</font>
<font color=#FF0000>}</font>
<font color=#990000>~</font><b><font color=#000000>Counter</font></b> <font color=#990000>(</font><font color=#990000>)</font><font color=#990000>;</font>

<font color=#009900>int</font> <b><font color=#000000>inc_counter_by_one</font></b> <font color=#990000>(</font><font color=#990000>)</font>
<font color=#FF0000>{</font>
<b><font color=#000000>old</font></b> <font color=#990000>(</font><font color=#009900>int</font> old_counter <font color=#990000>=</font> _counter<font color=#990000>)</font><font color=#990000>;</font>
_counter<font color=#990000>+</font><font color=#990000>+</font><font color=#990000>;</font>
<b><font color=#000000>ensure</font></b> <font color=#990000>(</font>_counter <font color=#990000>=</font><font color=#990000>=</font> old_counter <font color=#990000>+</font> <font color=#993399>1</font><font color=#990000>)</font><font color=#990000>;</font>
<font color=#FF0000>}</font>

<b><font color=#0000FF>private</font></b><font color=#990000>:</font>
<font color=#009900>int</font> _counter<font color=#990000>;</font>
<font color=#FF0000>}</font><font color=#990000>;</font></tt>
</pre>

With this mecanism you can decorate the source code with pre and post-conditions, or you can easily get rid of it in a release compiled version.

Concerning your macro example, I never use conditional expressions! It leads to side-effects :)

--
Globos
 
(Sorry for the preceeding post)
Thanks PerFnurt,

my problem is solved, I was confused by the use of macros.
But I know why I am defining such a macro! :)
I want to emulate the 'old' mecanism of Eiffel.
For example consider a basic Counter class:

#ifdef DBC
# define old(decl) decl
# define ensure(expr) assert (expr)
#else
# define old(decl)
# define ensure(expr)
#endif

class Counter
{
public:
Counter (int init) :
_counter (init)
{
}
~Counter ();

int inc_counter_by_one ()
{
old (int old_counter = _counter);
_counter++;
ensure (_counter == old_counter + 1);
}

private:
int _counter;
};

With this mecanism you can decorate the source code with pre and post-conditions, or you can easily get rid of it in a release compiled version.

Concerning your macro example, I never use conditional expressions! It leads to side-effects :)

--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top