By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

precedence of "using" and #include

P: n/a
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
Sep 23 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Steve Pope wrote:
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;
}
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
Sep 23 '06 #2

P: n/a
Steve Pope wrote:
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
Sep 23 '06 #3

P: n/a
benben <benhonghatgmaildotcom@nospamwrote:
>Steve Pope wrote:
>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::
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;
Sep 23 '06 #4

P: n/a
"Steve Pope" <sp*****@speedymail.orgwrote in message
news:ef**********@blue.rahul.net...
benben <benhonghatgmaildotcom@nospamwrote:
>>Steve Pope wrote:
>>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::

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
Sep 24 '06 #5

P: n/a
In article <ef**********@blue.rahul.net>, sp*****@speedymail.org says...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 24 '06 #6

P: n/a
Jerry Coffin <jc*****@taeus.comwrote:
>In article <ef**********@blue.rahul.net>, sp*****@speedymail.org says...
>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.
Thanks, Jerry (and also benben and Stu).

Steve
Sep 24 '06 #7

P: n/a
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.

--

Frederick Gotham
Sep 24 '06 #8

P: n/a
Frederick Gotham <fg*******@SPAM.comwrote:
>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.
Since "using" is not a keyword in C, I don't see how this applies.

Cheers
Steve
Sep 24 '06 #9

P: n/a
"Steve Pope" <sp*****@speedymail.orgwrote in message
news:ef**********@blue.rahul.net...
Frederick Gotham <fg*******@SPAM.comwrote:
>>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.

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
Sep 24 '06 #10

P: n/a
Steve Pope posted:
Frederick Gotham <fg*******@SPAM.comwrote:
>>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.

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 |
| |
-------------------

--

Frederick Gotham
Sep 24 '06 #11

P: n/a
Stuart Golodetz <sg*******@dNiOaSl.PpAiMpPeLxE.AcSoEmwrote:
>"Steve Pope" <sp*****@speedymail.orgwrote in message
>Frederick Gotham <fg*******@SPAM.comwrote:
>>>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.
>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.
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

Sep 24 '06 #12

P: n/a
"Steve Pope" <sp*****@speedymail.orgwrote in message
news:ef**********@blue.rahul.net...
Stuart Golodetz <sg*******@dNiOaSl.PpAiMpPeLxE.AcSoEmwrote:
>>"Steve Pope" <sp*****@speedymail.orgwrote in message
>>Frederick Gotham <fg*******@SPAM.comwrote:
>>>>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.
>>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.

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
Sep 25 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.