Joy
Joy

Reputation: 1777

std::string vs string

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

Upvotes: 0

Views: 4905

Answers (6)

Dimitri Sabadie
Dimitri Sabadie

Reputation: 1

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

Upvotes: 0

Steve Jessop
Steve Jessop

Reputation: 279255

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

Upvotes: 9

juanchopanza
juanchopanza

Reputation: 227400

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;

Upvotes: 5

user520288
user520288

Reputation:

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.

Upvotes: 3

Dennis
Dennis

Reputation: 3731

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

Upvotes: 2

user267885
user267885

Reputation:

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

Upvotes: 4

Related Questions