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.
4 Answers
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.
4 Comments
using std::string; at least makes it clear that "I need one specific part a lot", rather than "lololo, bring teh codez on". (Still, don't put it in headers)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.
Comments
You could typedef the classes you wish to use, but this is a really bad idea.
#include <string>
typedef std::string string;
5 Comments
using std::string; is the preferred way of bringing string into the current namespace.using, and it's not good to pollute a header with using, either. Although, saying that, I'm just polluting with a different typename instead.using?
using namespace stdand no, because it's a terrible idea