473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

precedence of "using" and #include

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
12 2383
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
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
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
"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
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
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
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
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
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Jalal | last post by:
I am trying to use numeric_limits<double>::min() in an MFC application in Microsoft Visual C++.NET 2003. I am having some difficulties here. The following are the parts of a simple program I wrote...
5
by: Jacky Yuk | last post by:
Hi all, I am new to c++ but using c for long time. Recently, I created a MFC GUI project by VC/C++ 6.0. Everything was fine until I wanted to use "template": template <typename T> class...
32
by: Mike Machuidel | last post by:
Hi, I'm a game developer programming mostly in C and ASM for about 7 years. Today at work a colleague (a C++ programmer) yelled at me I'm a bad C programmer because I use "return(0);" instead...
3
by: ARZ | last post by:
Hi How do I include DLLs based on a condition. Eg., there are 2 DLLs 'Oracle.dll' and 'SQL.dll'. I need to include ANYONE of the DLLs based on a value. Is it possile in C# Thanks in advance ARZ
9
by: bubbakittee | last post by:
I am designing a package of functions to be used by people with very little programming experience (if any) and very little patience. I don't want to scare them with programmerish words like...
6
by: AlexD_UK | last post by:
When I create a new C++ project of type "Class Library (.NET)", I am unable to then add the following line of code : using namespace std If I do, I get the following error on compilation :...
43
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
10
by: Eric | last post by:
Hello, I have some server side includes on a Classic asp page that look something like: <!-- #include virtual="/includes/file1.asp"--> <!-- #include virtual="/includes/file2.asp" --> <!--...
15
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.