I have seen codes with using namespace std;. Does it mean that if we use this then we don't have to include header files in the code or if we don't use namespaces, does it mean that we have to use std:: before every function, class?
You have to include header files and use namespaces.
The namespaces are contained in the header files, io streams like cin,cout are contained in the namespaces.So, only if you include the header file, namespace can be used. Without using namespace std, You have to use scope resolution operator every time you use those functions.
using namespace std; means that all names from std namespace can be used without specifying their namespace explicitly (with std:: prefix). That is, after using namespace std;, both string and std::string are valid. Without using namespace std;, only std::string would work.
Header files still must be included.
Note that usage of using namespace is often discouraged as it populates your code with all names from that namespace, and conflicts may occur.
using namespace std;
Is not really an ideal practice I would apply in a professional code base. The reason is that it practically "opens up" std namespace (packages in Java if you like) where you are probably doing "Hello world"ish programming i.e. not so severe as RT Embedded, Mission Critical, or Safety Critical. For example, I work in Interservice/Industry Training and Simulation where things are often safety/mission critical; people will propbably have a quiet word with me if I was using multiple namespaces so openly. It's not really about the size of your program, it is more about Good practice. Yes, if you have so many things to use from std namespace, then probably you can simply use it. A compromise, and also what I sometimes do, is:
using std::endl;
using std::string;
using std::cout;
using std::cin;
// And something like that
This "exposes" the ones that you will need for this scope and still lets you use:
string myStr;
cout << "Some cout" << endl;
Like what you mentioned in your question. Why not try that?
A "Good bit" of all is that if you follow the approach I mentioned, it also "Upgrade" your level of knowlege in C++ namespaces and possible STLs.
I know some people will say "Well that is stil hard work" but to me it is a good compromise, up to a point. :)
DON'T FORGET TO ADD THE NECESSARY HEADER FILES PLEASE :-)
Related
Recently i've noticed that if I don't include the 'using namespace std' in my program and just use 'std::cout' or 'std::endl' then my program still compiles just fine? Therefore if I use the 'std::' prefix is it even necessary to include the 'using namespace std'? Does the 'std::' prefix call the using namespace std? If someone can clear this up for me it would be greatly appreciated.
In C++, :: is called the "scope resolution operator", so std::cout means "The variable cout, which can be found inside std".
The using namespace std; directive means "For any name you can't find, also try looking it up in std". Thus, if you explicitly say std::cout (and equivalent for everything else in the std namespace), then you don't need using namespace std.
As others have said, it's usually considered bad style to use using namespace std;, because a lot of the names in std are things you might want to use elsewhere, for example std::count. A compromise option is to instead say using std::cout;, which will tell the compiler to look for cout in std but not anything else.
There's debate about whether to say using std::cout or just write std::cout everywhere you need it in your main C++ file, but most people believe that you should rarely if ever use using namespace. If you're writing a header file, though, you should never use a using directive at the top level because then every file which includes yours will get it, and they might want to use different names.
When you use:
using namespace std;
You're telling the program that anything that it can't find should be in the namespace std. For larger programs, this is a problem because you might be using multiple namespaces with functions that have the same name, so it's best to use std::cout for larger programs.
I searched but I could not find related question.
Please correct me if I'm wrong.
In my project I use the following:
using namespace std;
using namespace sf;
I want it to be like below.
using namespace std, sf;
Thanks in advance!
This syntax is not supported, so you'll have to keep declaring multiple using statements.
In general, though, it's considered best practice to avoid declaring using namespace at all - definitely not in headers, and preferably in the inner-most scope possible (as to not pollute too much scope with unwanted symbols.
I want it to be like below.
using namespace std, sf;
The syntax you're after is simply not supported by the current c++ standard.
Besides it is discouraged to import whole namespaces (at least not in header files), you may sent a request to the c++ standards committee, and see if they like to support that.
The general advice is that you should only
either specify exact classes you're using in your translation unit (to save typing) like using std::cout = co;
or to make everything clear by explicitly using fully qualified identifiers everywhere like std::cout, std::endl, etc.
The latter way is the most readable and best IMO.
I am curious if there is any header file in C++ which can be used (included) instead of the standard namespace (namespace std) that works the same even in new versions of C++? I want to know if I can write code without using any namespaces and still be able to use the string data type.
string is in the std namespace, so you can't completely disregard it.
There are options though:
using std::string;
using namespace std;
typedef std::string myString;
//or fully qualify the name
std::string mystr;
which you can put in a header and include that.
There, now I gave you the recipe for disaster. Don't use it!
Namespaces are good. Learn to use them, rather than hacking your way around them.
Headers and namespaces are not related, and namespaces are good things. using namespace std is bad. You can always use the std::string data type without using namespace std;.
To use "using namespace std;" is a poor idea (although I have to admit I do this rather regularly in my samples I post here, for ease of typing). To hide the same in a header file is an even worse idea.
Namespaces are there for a reason.
But if you have, say, 100000 lines of already existing code that is written pre-namespace standard, and you quickly want to port that to use in a new compiler, then adding "using namespace std;" to the top of each file would be the preferred solution.
You could typedef the classes you wish to use, but this is a really bad idea.
#include <string>
typedef std::string string;
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why is 'using namespace std;' considered a bad practice in C++?
Every time I use using namespace std I always get that "thats a terrible programming habit".
Now I'm graduating this December with my B.S. in C.S. but I don't claim to know everything, but no one has ever explained why this is so bad. I understand what it does but I honestly don't see a huge deal with it.
Anyone care to explain? In my mind it just makes typing cout a whole lot more bearable than std::cout.
I can understand why you wouldn't want to put it in a header file, but just in a normal implementation file... I dont see why it would be a problem.
There is no problem using using namespace std in your source file when you make heavy use of the stl and know for sure that nothing will collide.
However, very often you don't need to use using namespace std or not in the entire file:
Did you know you can:
void somefunction()
{
// Use it in a particular scope
using namespace std;
cout << "test" << endl;
}
found this useful post elsewhere:
Namespaces separate and organize functionality. You can have a xander333::sort() function and it won't conflict with std::sort() or boost::sort() or any other sort(). Without namespaces there can be only one sort().
Now let's say you've put "using namespace std;" in all your source files and you've implemented a simple templated function called fill() in the global namespace of one of your files. This file also depends on a header from libFoo -- foo.hpp. Version 2.1 of libFoo comes out and all of a sudden your program no longer compiles. You version of fill() suddenly conflicts with another fill()! What happened?
It turns out that the folks implementing libFoo included in the new version of foo.hpp when they didn't before. Now you have all of the standard algorithms being included in your source file, and your using namespace std; has pulled them all into the global namespace. std::fill() now directly conflicts with your fill().
More insidious, you've gotten your code to compile by renaming your fill() to xander333_fill(), but something's not working right -- your report numbers are off. It turns out that your custom divides() function, which does fixed precision math, is no longer being called because the templated function from (also newly included by foo.hpp) makes for a better match because you're calling types did not exactly match the declared types.
Thread with relevant discussion is here:
http://www.cplusplus.com/forum/unices/27805/
a "good practice" that I am aware of is not to put using namespace in include files, but be free to use it to your taste in your private .cpp files. I know people who like everything to be fully qualified, and some (like me) who assume that string is an std::string unless stated otherwise.
The reason for this is that if/when others use your include file (and this happens always), they are forced to accept your programming style.
Good luck!
My preference is to:
never put a using directive in a header file (the things that include your header may not like the fact that you forced them to have the using directive).
always do things like using std::cout; at the top of the implementation files so I don't have to do std::cout everywhere in my code.
It's primarily about good housekeeping. If you're not really going to use more than a few identifiers in a namespace, why clutter up your own namespace by dumping all of the identifiers from that namespace into yours? It's preferable to use using std::cout. However, if you use a namespace very heavily and it doesn't cause any collisions, go ahead and use using namespace.
Another reason to not use using other than avoiding potential naming collisions is to speed up your IDE and possibly compiles.
If you're using Visual Studio, using namespace std and/or using namespace boost can kill intellisense entirely. There are a lot of symbols in these namespaces that you may not realize and dumping them into the global namespace can be ludicrous.
Avoiding using statements for entire namespaces helps prevent unintentional conflicts between libraries. Supposed you made your own class that had the same name as something in std, then unless you explicitly use std:: you will have name conflicts.
It's probably best to try to avoid conflicts like this in the first place, but if you specify the namespace for each member in your code it will be less ambiguous.
If you get tired of typing std::cout all the time, you can use a using statement for just that member.
I have a C++ project (VC++ 2008) that only uses the std namespace in many of the source files, but I can't find the "right" place to put "using namespace std;".
If I put it in main.cpp, it doesn't seem to spread to my other source files. I had it working when I put this in a header file, but I've since been told that's bad. If I put it in all of my .cpp files, the compiler doesn't recognize the std namespace.
How should this be done?
You generally have three accepted options:
Scope usage (std::Something)
Put using at the top of a source file
Put using in a common header file
I think the most commonly accepted best practice is to use #1 - show exactly where the method is coming from.
In some instances a file is so completely dependent on pulling stuff in from a namespace that it's more readable to put a using namespace at the top of the source file. While it's easy to do this due to being lazy, try not to succumb to this temptation. At least by having it inside the specific source files it's visible to someone maintaining the code.
The third instance is generally poor practice. It can lead to problems if you're dependent on more than one external source file both of which may define the same method. Also, for someone maintaining your code it obfuscates where certain declarations are coming from. This should be avoided.
Summary:
Prefer to use scoped instances (std::Something) unless the excessive use of these decreases the legibility and maintainability of your code.
In your headers, the best thing I think is just to fully qualify the namespace, say of a member
#include <list>
class CYourClass
{
std::list<int> myListOfInts;
...
};
You can continue to fully qualify in any function in a cpp
int CYourClass::foo()
{
std::list<int>::iterator iter = myListOfInts.begin();
...
}
you really never need "using namespace std" anywhere. Only if you find you are typing std:: too much, thats when its good to throw in a "using namespace std" to save your own keystrokes and improve the readability of your code. This will be limited to the scope of the statement.
int CYourClass::foo()
{
using namespace std;
list<int>::iterator iter = myListOfInts.begin();
...
}
You can put it in multiple places - in each .cpp file.
Essentially, you need to make a choice:
1) "do the right thing" by including using namespace std in all your cpp files, or
2) add using namespace std in some common header file.
Different people will have different opinions of which is best, however if you ask me, I'd chose to put using namespace std in a common header. In my opinion, the std namespace is just that - "standard" and any name conflicts need to be resolved at code level, rather than relying on namespaces.
The namespace doesn't 'spread' to other files.
You have to put it in each of the files, or just explicitly call out your classes.
either:
using namespace std;
blah << x;
or:
std::blah << x;
The choice is a style choice, either works in practice.
If the compiler doesn't 'recognize' the namespace it's because you haven't included the definition file that declares it ( i.e. include )
It's not a good idea to spread over your code using namespace std;only because it's seem convenient to you to do so. But if you insist on it you can wrap your own code into that name space. And only later when you will actually will make a use of your function/classes in the main file you will define using namespace std;. Just to emphasize that I'm saying, here the example:
namespace std
{
class MyNewClass
{
public:
MyNewClass( ) { out << "Hello there" << endl;};
};
};
int main( )
{
std::MyNewClass tmp;
};