0

I'm using in my code many namespaces including the std one , so when I want to declare a string variable in my code should I precise std::string or I can just put string :

#include <string.h> 

using namespace std;
using namespace boost;
using namespace xerces;

int main()
{
    /*! should I declare my str like this */
    std::string str;
    /*! or I can declare it like this */
    string str1;
    cout << str << str1 <<endl;
    return 0;
}
9
  • 3
    That is what using is for :) Commented May 4, 2012 at 10:28
  • 7
    #include <string.h> doesn't bring you std::string declaration Commented May 4, 2012 at 10:32
  • 6
    Why is 'using namespace std;' considered a bad practice in C++? Commented May 4, 2012 at 10:35
  • 2
    @Glolita: StackOverflow isn't going to give you a definitive "yes/no" answer to a question that's a matter of style. Commented May 4, 2012 at 10:48
  • 2
    @juergend: No. That is how using is abused. Commented May 4, 2012 at 11:03

6 Answers 6

9

Since you have using namespace std;, the name string means the same as std::string[*]. It's therefore a question of style which you prefer (and if you prefer std::string then you can leave out using namespace std;).

There are some name clashes between std:: and boost::, in particular for things that were trialled in Boost prior to standardization. So for example if you include the appropriate headers then both std::shared_ptr and boost::shared_ptr exist. They may or may not refer to the same type, I haven't checked whether Boost tries to detect the standard type before defining its own.

So it's not necessarily a good idea to use both std and boost namespaces at the same time. You can use individual names with using std::string;, instead of the whole namespace.

[*] if std::string is defined, which it isn't, since you didn't include <string>.

Sign up to request clarification or add additional context in comments.

2 Comments

ok I already got that, but what if I needed in my code two classes from two different namespaces that have the same name ( but of course not the same implementation, let's say my code is a switching class or something like that) so I need to precise the namespace name before my called class , but once I did that shouldn't I keep doing it even with the classes that doesn't have a similar name in the other used namespaces ???
@Glolita: it's entirely up to you (or your boss, if your boss wrote your style guide). Just because some names are ambiguous, and therefore require a namespace prefix, doesn't mean that you have to write a namespace prefix on every name. If you want to write string, and it doesn't clash with any other definition of string, then first do either using namespace std; or using std::string;. I prefer using std::string;, because I don't like to import a great long list of names, some of which I've never heard of, and that might change in a future compiler release.
5

You can just write string. But what if boost or xerces also have a symbol string? I would advise against using these using directives. It is not only string that could clash. You are essentially pulling a whole lot of symbols into the global namespace. If you really want to avoid typing std:: then you can use a typedef:

typedef std::string MyStr;

2 Comments

Or worse: what if you type strong, meaning to type std::string and there's a boost::strong?
@R.MartinhoFernandes good point. Or if you have a templated set function, as in a question I saw recently.
4

You can put just string if you use using namespace std;.

Adding using namespace std; may not be the best idea in all cases, because it can lead to conflicts between namespaces in some cases (though unlikey for string).

Comments

3

Usually it is not needed to specify std::string if you have declared using namespace std; BUT as a general case, if there are multiple namespaces which contain different classes with the same name, then you will have to specify the namespace next to the type (namespace::type) regardless of the existence of the using statement.

Comments

2

You are using the namespace std so you do not NEED to prepend string with std:: but you CAN if you want.

Comments

0

A good way to code is not to use any relevant namespace in headers, in order to prevent exposing the namespaces outer when #include. But in compiled source, you can do whatever you want, even using the std namespace and call std::string. Sometimes it's even needed (if you include two namespaces that define the same string class).

Comments

Your Answer

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.