0

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.

asked Jan 29, 2013 at 11:44
10
  • 1
    Why don't you want to add namespaces? Commented Jan 29, 2013 at 11:44
  • 4
    Don't use using namespace std and no, because it's a terrible idea Commented Jan 29, 2013 at 11:45
  • I'm guessing, because there is a bunch of old code that doesn't have namespace qualifiers. Commented Jan 29, 2013 at 11:45
  • I want it to be compatible with old C++ code but I can't use <iostream.h> because my compiler is not old. Commented Jan 29, 2013 at 11:48
  • 2
    The only way to fix old code is to fix it. Commented Jan 29, 2013 at 11:50

4 Answers 4

4

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.

answered Jan 29, 2013 at 11:46
Sign up to request clarification or add additional context in comments.

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)
What is the difference between "std::string MyString" and "typedef std::string MyString" in functionality?
@MohammadSanei the first creates a variable, the second defines a type.
So then I'll be able to use MyString as a shorthand for std::string?
2

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;.

answered Jan 29, 2013 at 11:45

Comments

2

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.

answered Jan 29, 2013 at 11:47

Comments

0

You could typedef the classes you wish to use, but this is a really bad idea.

#include <string>
typedef std::string string;
answered Jan 29, 2013 at 11:47

5 Comments

using std::string; is the preferred way of bringing string into the current namespace.
Yes, I know. But they didn't want to use 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.
That's perfect! I just got what you were saying! Is there any way to define cout and endl in a similar way inside a header file without polluting it with using?
It's a perfectly terrible idea.
Is there any way to define cout in a similar way?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.