Should I make namespace std global?
-
Should I do this: #include <iostream> using namespace std; or should I do this: #include <iostream> using std::cout; using std::cin; using std::endl; . . . I find that making the namespace std global in implementation file easier to work with, but some people suggest avoid doing that. Should I? Do you make namespace std global? Thanks, Alex ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
-
Should I do this: #include <iostream> using namespace std; or should I do this: #include <iostream> using std::cout; using std::cin; using std::endl; . . . I find that making the namespace std global in implementation file easier to work with, but some people suggest avoid doing that. Should I? Do you make namespace std global? Thanks, Alex ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
The second option is neater, and better style. In the real world, it's doubtful that you'll notice the difference. I always do the second, to be a nerd. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
Should I do this: #include <iostream> using namespace std; or should I do this: #include <iostream> using std::cout; using std::cin; using std::endl; . . . I find that making the namespace std global in implementation file easier to work with, but some people suggest avoid doing that. Should I? Do you make namespace std global? Thanks, Alex ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
I rarely use either. The whole point of namespaces is to be able to separate groups of things to avoid name clashes. The
using
directive basically gets rid of the namespaces and puts everything in the global namespace, making the use of the namespace pointless, especially the first form. If I do use theusing
directive, I use the second form. It's a lot cleaner and helps to prevent clutter of the global namespace.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Should I do this: #include <iostream> using namespace std; or should I do this: #include <iostream> using std::cout; using std::cin; using std::endl; . . . I find that making the namespace std global in implementation file easier to work with, but some people suggest avoid doing that. Should I? Do you make namespace std global? Thanks, Alex ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
Okay, so if I use using namespace std;, does that mean the program will compile everything named under namespace std? Many people recommend not to make the namespace std global, but some say it doesn't matter. But I usually do the second way, typing explicitly using::string, using::cout......until I read the newly released book from Andrei Alexandrescu (highly repected person in the field), the book named C++ coding standard. (ISBN: 0-321-11358-6) Here is a quote from the book: But here's the common trap: Many people think that using declarations issued at namespace level (for example, using N::Widge) are safe. They are not. They are at least as dangerous, and in a subtler and more insidious way. After reading this, I was thinking, am I doing the right way? So I thought I would ask people here how they are doing it.
// Implementation file #include <iostream> using namespace std; int main() { // code return 0; }
or should I do this:// Implemenation file #include <iostream> using::cout; using::cin; using::endl; int main() { //code return 0; }
----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented". -
Okay, so if I use using namespace std;, does that mean the program will compile everything named under namespace std? Many people recommend not to make the namespace std global, but some say it doesn't matter. But I usually do the second way, typing explicitly using::string, using::cout......until I read the newly released book from Andrei Alexandrescu (highly repected person in the field), the book named C++ coding standard. (ISBN: 0-321-11358-6) Here is a quote from the book: But here's the common trap: Many people think that using declarations issued at namespace level (for example, using N::Widge) are safe. They are not. They are at least as dangerous, and in a subtler and more insidious way. After reading this, I was thinking, am I doing the right way? So I thought I would ask people here how they are doing it.
// Implementation file #include <iostream> using namespace std; int main() { // code return 0; }
or should I do this:// Implemenation file #include <iostream> using::cout; using::cin; using::endl; int main() { //code return 0; }
----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".Alex Ngai wrote: But here's the common trap: Many people think that using declarations issued at namespace level (for example, using N::Widge) are safe. They are not. They are at least as dangerous, and in a subtler and more insidious way. Which is exactly why I rarely use them either. Promoting any namespace member to the global namespace (which is what the
using
directive actually does) has the rather dangerous ability to shadow a member already in the global namespace with the same name, much like an overloaded function, but a lot more subtle. These errors can be very difficult to debug.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Alex Ngai wrote: But here's the common trap: Many people think that using declarations issued at namespace level (for example, using N::Widge) are safe. They are not. They are at least as dangerous, and in a subtler and more insidious way. Which is exactly why I rarely use them either. Promoting any namespace member to the global namespace (which is what the
using
directive actually does) has the rather dangerous ability to shadow a member already in the global namespace with the same name, much like an overloaded function, but a lot more subtle. These errors can be very difficult to debug.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Thanks for your reply. But many feature in stardard C++ are in namespace std. Even the simple input & output, string are in namespace std. So, should I stick with using::string style? Is it better or it doesn't matter, like what the author said? ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
-
Thanks for your reply. But many feature in stardard C++ are in namespace std. Even the simple input & output, string are in namespace std. So, should I stick with using::string style? Is it better or it doesn't matter, like what the author said? ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
My preference is to use individual
using std::string;
type statements instead of bringing the whole std namespace in withusing std;
But of course there is no reason why you have to useusing ...;
at all. You can just use the full names at the appropriate points. egstd::cout << "This is a test" << std::endl;
Instead ofusing std::cout; using std::endl; cout << "This is a test" << endl;
Mike -
My preference is to use individual
using std::string;
type statements instead of bringing the whole std namespace in withusing std;
But of course there is no reason why you have to useusing ...;
at all. You can just use the full names at the appropriate points. egstd::cout << "This is a test" << std::endl;
Instead ofusing std::cout; using std::endl; cout << "This is a test" << endl;
MikeMike Beckerleg wrote: You can just use the full names at the appropriate points. Indeed, I prefer to use the explicit full names. It is then obvious which function you intend to call. Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fruity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Walliams (Little Britain) -
Mike Beckerleg wrote: You can just use the full names at the appropriate points. Indeed, I prefer to use the explicit full names. It is then obvious which function you intend to call. Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fruity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Walliams (Little Britain)Antony M Kancidrowski wrote: Indeed, I prefer to use the explicit full names. It is then obvious which function you intend to call. Exactly :)
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"