Archived

This topic is now archived and is closed to further replies.

Ekim_Gram

std:: vs. using

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
I personally use using for stuff like this :


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.

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites