std:: vs. using
I haven''t seen any topics on this around so I''m going to ask it. Which would be a better choice to use within a project? I know it would make no difference whatsoever but would the compile time be faster? Would the file size be any larger if you use the using keyword? I know it''s much easier to type using std::cout or std::cin at the top of the source file rather than typing std::cout and std::endl etc. over and over again. But would it change performance or anything like that?
R.I.P. Mark Osback
Solo Pa Mi Gente
No difference at all. I even compiled a test app for both cases and file size came out the same. (God, Dev-C++ takes a long time to compile on a P-200. )
of course it makes no difference. Its just a language thing, nothing to do with the result.
It make absolutely no difference in the end executable.
The only things using-directives and using-declaration change is the way names are looked up during compilation. i.e. you''re instructing the compiler that it should also consider, for example std::cout when you''re telling it about cout.
The reason why it is advised to always use fully-qualified names (std::cout) in headers instead of relying on using-directives, is that a using-directive in a header file will have effect on all the files that #include it.
That means you are breaking the namespace barriers for everybody, whether they want it or not. It may very well happen that some modules would then not compile because some of the names they use conflict with a name you unwittingly imported (there are a lot of things in the standard namespace).
The module author may have been careful only to import those names he needed, but you went and blew everything up.
Using FQN in headers is no big hassle. You get used to it after a time. Within a source file, especially one you maintain, namespaces are fair game.
Remember, they are here to help you, not to hinder you.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
The only things using-directives and using-declaration change is the way names are looked up during compilation. i.e. you''re instructing the compiler that it should also consider, for example std::cout when you''re telling it about cout.
The reason why it is advised to always use fully-qualified names (std::cout) in headers instead of relying on using-directives, is that a using-directive in a header file will have effect on all the files that #include it.
That means you are breaking the namespace barriers for everybody, whether they want it or not. It may very well happen that some modules would then not compile because some of the names they use conflict with a name you unwittingly imported (there are a lot of things in the standard namespace).
The module author may have been careful only to import those names he needed, but you went and blew everything up.
Using FQN in headers is no big hassle. You get used to it after a time. Within a source file, especially one you maintain, namespaces are fair game.
Remember, they are here to help you, not to hinder you.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
I personally use using for stuff like this :
Otherwise, as stated before, it''s just a compile time thing.
bool func(){ /* some code */ if (FAILED(hResult)) // hello DirectX { using namespace Debug; switch (hResult) { /* ... */ DoDebugStuff(); break; } return false; } return true;}
Otherwise, as stated before, it''s just a compile time thing.
Sure it differs, this does not work:
But this does:
[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
#include <list>using namespace std;void foo(int list){ list<int> bar;}
But this does:
#include <list>void foo(int list){ std::list<int> bar;}
[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
As I said, it changes the rules for symbol lookup, possibly changing the meaning of the program.
But by themselves, using directives and statements do not affect a program''s performance in any way.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
But by themselves, using directives and statements do not affect a program''s performance in any way.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement