Topic: where to put using namespace
Author: aa <qwe321@live.co.uk>
Date: Tue, 22 Jun 2010 16:41:16 CST Raw View
Hi,
I haven't used C++ for some time, when I last used it STL was just
gaining in popularity and I remember typing using namespace std; at
the top of my C++ source files.
However I've just seen an example where the using namespace was
entered in the int main function!
int main()
{
using namespace std;
...
}
Can anyone explain to me the reason for this change? Why put the using
namespace clause inside a function rather than at the top of the
source file?
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: cpp4ever <n2xssvv.g02gfr12930@ntlworld.com>
Date: Wed, 23 Jun 2010 13:14:06 CST Raw View
On 06/22/2010 11:41 PM, aa wrote:
> Hi,
>
> I haven't used C++ for some time, when I last used it STL was just
> gaining in popularity and I remember typing using namespace std; at
> the top of my C++ source files.
>
> However I've just seen an example where the using namespace was
> entered in the int main function!
>
> int main()
> {
> using namespace std;
> ...
> }
>
> Can anyone explain to me the reason for this change? Why put the using
> namespace clause inside a function rather than at the top of the
> source file?
>
> --
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use
> mailto:std-c++@netlab.cs.rpi.edu <std-c%2B%2B@netlab.cs.rpi.edu><
std-c%2B%2B@netlab.cs.rpi.edu <std-c%252B%252B@netlab.cs.rpi.edu>>
> ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
>
That syntax is used to restrict the scope of where the std namespace is
made available without requiring an explicit reference. See the example
below
#include <vector>
int another()
{
// Must explicitly use std::vector<int> as namespace
// std in not in the global or another() function scope
std::vector<int> v;
v.push_back(-2);
// Other code after here
}
int main()
{
// The using below bring namespace std into the the scope of the
// main function
using namespace std
// No need to explicitly use std::vector<int> as the the
// namespace is in the scope of the main function
vector<int> v;
v.push_back(2);
// Other code after here
}
The old example you remember would bring the std namespace into the
scope of that source file. Hence all std namespace functions in that
file would not require explicit usage.
This is an aspect of C/C++ that I think is rarely covered well enough,
and I hope this brief explanation helps.
JB
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =3D?ISO-8859-1?Q?Daniel_Kr=3DFCgler?=3D <daniel.kruegler@googlemail.c=.om>
Date: Wed, 23 Jun 2010 13:13:15 CST Raw View
On Jun 23, 12:41 am, aa <qwe...@live.co.uk> wrote:
> Hi,
>
> I haven't used C++ for some time, when I last used it STL was just
> gaining in popularity and I remember typing using namespace std; at
> the top of my C++ source files.
This is still allowed (even though not generally recommended).
> However I've just seen an example where the using namespace was
> entered in the int main function!
>
> int main()
> {
> using namespace std;
> ...
> }
>
> Can anyone explain to me the reason for this change?
This is no change, but another way to define a using-directive.
This syntax-form was already part of the 1998 standard of
C++. A using-directive can occur either in namespace scope
(your example) or in block scope, see [namespace.udir]/1:
"A using-directive shall not appear in class scope, but may
appear in namespace scope or in block scope.[..]"
> Why put the using namespace clause inside a function
> rather than at the top of the source file?
Well, to prevent unnecessary pollution of names in the global
scope! With this form of a using-directive you can intentionally
reduce the scope of the directive to those places where you
really need it.
HTH & Greetings from Bremen,
Daniel Kr=FCgler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Wed, 23 Jun 2010 13:13:28 CST Raw View
aa wrote:
> Hi,
>
> I haven't used C++ for some time, when I last used it STL was just
> gaining in popularity and I remember typing using namespace std; at
> the top of my C++ source files.
>
> However I've just seen an example where the using namespace was
> entered in the int main function!
>
> int main()
> {
> using namespace std;
> ...
> }
>
> Can anyone explain to me the reason for this change? Why put the using
> namespace clause inside a function rather than at the top of the
> source file?
>
>
As a general principle confine names to the smallest scope necessary. This
limits name pollution. If you use large source code files, limiting the
effect of a using directive makes sense but it also makes sense to keep your
source code files small when a using directive will have less chance of
causing a problem.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Stanley Friesen <sarima@friesen.net>
Date: Wed, 23 Jun 2010 13:13:44 CST Raw View
aa <qwe321@live.co.uk> wrote:
>Hi,
>
>I haven't used C++ for some time, when I last used it STL was just
>gaining in popularity and I remember typing using namespace std; at
>the top of my C++ source files.
>
>However I've just seen an example where the using namespace was
>entered in the int main function!
>
>int main()
>{
> using namespace std;
> ...
>}
>
>Can anyone explain to me the reason for this change? Why put the using
>namespace clause inside a function rather than at the top of the
>source file?
Limitation of scope interference. Done this way the names declared in
the namespace are not introduced into the global namespace, but just
into the one function. This greatly reduces the chances of name
conflicts.
--
The peace of God be with you.
Stanley Friesen
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Mathias Gaunard <loufoque@gmail.com>
Date: Thu, 24 Jun 2010 12:30:44 CST Raw View
On Jun 22, 11:41 pm, aa <qwe...@live.co.uk> wrote:
> Hi,
>
> I haven't used C++ for some time, when I last used it STL was just
> gaining in popularity and I remember typing using namespace std; at
> the top of my C++ source files.
A tad off-topic, but good practices are that you should never use
'using namespace' and should always qualify names (even when in the
same namespace).
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]