you know usingnamespace std in the global scope is a bad habbit, is it okay to put it in main or other blocks?

Nov 22, 2012 at 1:16pm
writing std: all the time is good practice i know but when im just putting something together to see if i learnt correctly its too tempting to just whack using namespace std at the top, bad habbit i know, specialy in practice of all places.

just theres not so likley going to be a namespace leak if you pop it between {} these right?

would that be a better habbit?
Nov 22, 2012 at 1:36pm
Remember you could do something like this inside main() or other function.
1
2
3
4
    using std::string;
    using std::printf;
    using std::cout;
    using std::endl;


That keeps the body of the code uncluttered as before, but gives much tighter control.
Nov 22, 2012 at 2:00pm
A better habit would be to use the "std::" prefix or a using declaration (like Chervil has provided above).
Last edited on Nov 22, 2012 at 5:22pm
Nov 22, 2012 at 2:13pm
closed account (z05DSL3A)
Prefer Using Declarations to Using Directive but prefer full scope resolution to Using Declarations.

See the following for the differences between Using Declarations and Using Directive:
http://www.cplusplus.com/forum/beginner/9181/#msg42419
Topic archived. No new replies allowed.