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;
}
Joy
  • 1,707
  • 7
  • 29
  • 44
  • 3
    That is what `using` is for :) – juergen d May 04 '12 at 10:28
  • 7
    `#include ` doesn't bring you `std::string` declaration – Tadeusz Kopec for Ukraine May 04 '12 at 10:32
  • 6
    [Why is 'using namespace std;' considered a bad practice in C++?](http://stackoverflow.com/a/1453605/140719) – sbi May 04 '12 at 10:35
  • @Tadeusz: and it certainly doesn't bring you namespace `xerces`! This code doesn't compile, the questioner has left out the real includes. – Steve Jessop May 04 '12 at 10:35
  • As a terminally lazy vim user, I have `imap std::` in my vimrc. Hit F9: get the `std::` for 1 key press. – BoBTFish May 04 '12 at 10:45
  • it's just a little part of the code that I'm using , I just focused on the string thing , I mean I'm even using other namespaces that I'd created and some classes hold same name as classes in those given namespace so I was wondering should I precise everytime the namespace before the called class or only just when I have similar name classes in these different namespaces ?? – Joy May 04 '12 at 10:46
  • 2
    @Glolita: StackOverflow isn't going to give you a definitive "yes/no" answer to a question that's a matter of style. – Steve Jessop May 04 '12 at 10:48
  • 2
    @juergend: No. That is how `using` is abused. – Martin York May 04 '12 at 11:03
  • 1
    @Glolita the only definitive rule is: don't ever put `using namespace x;` into a header file. – moooeeeep May 04 '12 at 11:17

6 Answers6

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

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • 1
    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 ??? – Joy May 04 '12 at 10:42
  • 1
    @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. – Steve Jessop May 04 '12 at 10:44
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;
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
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).

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.

2

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

taskinoor
  • 45,586
  • 12
  • 116
  • 142
Dennis
  • 3,683
  • 1
  • 21
  • 43
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).