precedence of "using" and #include

S

Steve Pope

Compiling the following works on my system:

file main.cpp:

#include <iostream>

namespace space {
int foo;
}

main()
{
using space::foo;
#include "fragment"
cout << foo << endl;
}

file fragment:

foo = 3;

My question is, is there any reason it should not work because
the "using" statement needs to be in the file "fragment"? (I
believe it should be okay but I'm trying to decipher a communication
from a developer in a different environment than mine, along
these lines.)

Thanks,
Steve
 
B

benben

Steve said:
Compiling the following works on my system:

file main.cpp:

#include <iostream>

namespace space {
int foo;
}

main()
{
using space::foo;
#include "fragment"
cout << foo << endl;
}

file fragment:

foo = 3;

#include <iostream>

namespace space {
int foo;
}

int main()
{
using space::foo;
foo = 3;
std::cout << foo << std::endl;
}
 
B

benben

Steve said:
Compiling the following works on my system:

file main.cpp:

#include <iostream>

namespace space {
int foo;
}

main()
{
using space::foo;
#include "fragment"
cout << foo << endl;
}

file fragment:

foo = 3;

My question is, is there any reason it should not work because
the "using" statement needs to be in the file "fragment"?

Yes. Your main function should return an int and you have to qualify
cout and endl with std::

ben
 
S

Steve Pope

benben said:
Steve Pope wrote:
Yes. Your main function should return an int and you have to qualify
cout and endl with std::

Okay, that wasn't my question, and my system is liberal on the
points you mention. Corrected code below. So let me ask again.
Does "using space::foo;" need to be in the file fragment? Thanks.

Steve

***********

file main.cpp:

#include <iostream>

namespace space {
int foo;
}

int
main()
{
using space::foo;
#include "fragment"
std::cout << foo << std::endl;
}

file fragment:

foo = 3;
 
S

Stuart Golodetz

Steve Pope said:
Okay, that wasn't my question, and my system is liberal on the
points you mention. Corrected code below. So let me ask again.
Does "using space::foo;" need to be in the file fragment? Thanks.

Steve

***********

file main.cpp:

#include <iostream>

namespace space {
int foo;
}

int
main()
{
using space::foo;
#include "fragment"
std::cout << foo << std::endl;
}

file fragment:

foo = 3;

As far as I know, the answer to your question is no. The reason being that
it gets compiled as if the file fragment were included at the point in
question, i.e. it gets compiled in exactly the same way as:

using space::foo;
foo = 3;
std::cout << foo << std::endl;

At the line foo = 3; the using declaration has already been seen, thus foo
refers to space::foo as desired.

HTH,
Stu
 
J

Jerry Coffin

Compiling the following works on my system:

file main.cpp:

#include <iostream>

namespace space {
int foo;
}

main()
{
using space::foo;
#include "fragment"
cout << foo << endl;
}

file fragment:

foo = 3;

My question is, is there any reason it should not work because
the "using" statement needs to be in the file "fragment"?

No. By the time the compiler gets to the phase of analyzing the syntax,
such as figuring out the meaning of the individual statements (other
than preprocessor statements) it's just looking at a stream of text,
with the contents of the #include'd file where you've put it. If you
wanted to badly enough, you could even break a single statement across
several files:

/* ----------------------- main.c */
#include "file1.h"
#include "file2.h"
z;

/* ----------------------- file1.h */
int x

/* ----------------------- file2.h */
= y +

And when all is said and done, the compiler won't see this as being any
different from "x=y+z;" written altogether on a single line. Of course,
if you haven't defined y and z at that point, it'll complain just like
if you'd done it all together.

Of course, I'm not saying this is a good way to write your code --
rather the contrary, even though it makes no real difference to the
compiler, it's likely to make a big difference to anybody trying to read
a mess like this.
 
S

Steve Pope

No. By the time the compiler gets to the phase of analyzing the syntax,
such as figuring out the meaning of the individual statements (other
than preprocessor statements) it's just looking at a stream of text,
with the contents of the #include'd file where you've put it. If you
wanted to badly enough, you could even break a single statement across
several files:
/* ----------------------- main.c */
#include "file1.h"
#include "file2.h"
z;

/* ----------------------- file1.h */
int x

/* ----------------------- file2.h */
= y +
And when all is said and done, the compiler won't see this as being any
different from "x=y+z;" written altogether on a single line. Of course,
if you haven't defined y and z at that point, it'll complain just like
if you'd done it all together.
Of course, I'm not saying this is a good way to write your code --
rather the contrary, even though it makes no real difference to the
compiler, it's likely to make a big difference to anybody trying to read
a mess like this.

Thanks, Jerry (and also benben and Stu).

Steve
 
F

Frederick Gotham

Steve Pope posted:
My question is, is there any reason it should not work because
the "using" statement needs to be in the file "fragment"?


You should read a tutorial on the C Preprocessor. 90% of its functionality is
simple text replacement.
 
S

Stuart Golodetz

Steve Pope said:
Since "using" is not a keyword in C, I don't see how this applies.

Cheers
Steve

The C preprocessor works essentially the same way as the C++ one. It's
something that was inherited into C++ from C. What the code itself is
doesn't matter much; the preprocessor just replaces text, it doesn't care
what the text is.

Cheers,
Stu
 
F

Frederick Gotham

Steve Pope posted:
Since "using" is not a keyword in C, I don't see how this applies.


It applies because a source file goes through the preprocessor before it
goes through the "compiler proper".


----------------
Input | | Output
----> | Preprocessor | -----> V
| | |
---------------- |
V
|
V----<-------<------<--------<-----<----
|
|
| -------------------
| Input | |
----------> | Compiler Proper |
| |
-------------------
 
S

Steve Pope

The C preprocessor works essentially the same way as the C++ one. It's
something that was inherited into C++ from C. What the code itself is
doesn't matter much; the preprocessor just replaces text, it doesn't care
what the text is.

Sure, but the gist of my question is whether there's any chance
some C++ implementation might be processing the scope effects of
the "using" statements at the same time it is preprocessing, thus
leading to unexpected effects if a "using" is omitted from an
included file. While the answer is apparently "no", I'm asking
to see if anyone else has seen something like this.

Thanks,
Steve
 
S

Stuart Golodetz

Steve Pope said:
Sure, but the gist of my question is whether there's any chance
some C++ implementation might be processing the scope effects of
the "using" statements at the same time it is preprocessing, thus
leading to unexpected effects if a "using" is omitted from an
included file. While the answer is apparently "no", I'm asking
to see if anyone else has seen something like this.

Thanks,
Steve

In that case, the answer is no. Any implementation that does something
different is broken.

Regards,
Stu
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top