pragma

G

Guest

Hi,
I am new to C++. Could someone explain the meaning of below code:

# pragma warning(disable : 4786)
# define for if(0){}else for

Thanks
Tran Hong Quang
 
P

Peter Oliphant

# pragma warning(disable : 4786)

If error 4786 comes up while compiling it will be ignored and compilation
will continue (no warning will be given either)
# define for if(0){}else for

Don't think the above is even a valid derective. But it looks like the start
of a 'conditional-code block', but one that wouldn't be compiled since
"if(0)" is equivalent to "if(false)" which means the condition is false, and
so the block is not compiled.

[==P==]
 
B

Bruno van Dooren

I am new to C++. Could someone explain the meaning of below code:
# pragma warning(disable : 4786)
means that you don't want the compiler to generate warnings with code C4786
this is not a critical error, but if you want a clean compile without
warnings,
you have to disable some warnings in some cases.
# define for if(0){}else for
I think this is a weird construction, but what is does is it replaces all
instances of 'for' in your code with
'if(0){}else for'

example:

for(int i=0; i<10; i++)
cout << "test";

gets expanded to:
if(0){} else for(int i=0; i<10; i++)
cout << "test";

since if(0) is always false, the for loop is always executed, which is the
same as before.
in what context is this used?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
B

Bruno van Dooren

Don't think the above is even a valid derective. But it looks like the
start of a 'conditional-code block', but one that wouldn't be compiled
since "if(0)" is equivalent to "if(false)" which means the condition is
false, and so the block is not compiled.

This directive is valid. if you look at the preprocessed output, you'll see
that
the correct macro expansion is performed.

that doesn't mean it makes sense though, as the end result is identical to
whan you began with.

--

kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
D

David Wilkinson

Tran said:
Hi,
I am new to C++. Could someone explain the meaning of below code:

# pragma warning(disable : 4786)
# define for if(0){}else for

Thanks
Tran Hong Quang

1. Look up C4786 in the help to see what this warning relates to. This
warning often comes up when using the standard library, because expanded
identifiers can easily have more than 255 characters. You might as well
always turn this warning off, because not much can be done about it.

2. I think this is a trick for preventing the identifier i being
injected into the surrounding scope when you write, for example

for (int i=0; i<10; i++)
{
}

Some versions of VC do not adhere to the C++ standard, which says that i
should be local to the scope of the loop. This allows you to write

for (int i=0; i<10; i++)
{
}

for (int i=0; i<10; i++)
{
}

without getting error that i is multiply defined. Personally, this hack
makes me nervous and I tend to do

int i;
for (i=0; i<10; i++)
{
}

for (i=0; i<10; i++)
{
}

HTH,

David Wilkinson
 
A

Ahti Legonkov

Bruno said:
This directive is valid. if you look at the preprocessed output, you'll see
that
the correct macro expansion is performed.

that doesn't mean it makes sense though, as the end result is identical to
whan you began with.

It actually does make sense - this is a workaround to fix
non-conforming for loop.


for (int i; ;);

// In VC6 i is still in scope, but not with workaround.
// As expected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top