0

I'm relatively new to C++, and I have a simple question. Let's say I want to declare a stringstream object or any other. Is there any difference in using the fully-qualified name std::stringstream and just declaring the object as stringstream? I've seen it done both ways in code examples.

As I said, I'm pretty new to C++, so I'd love some clarification. Thanks!

  • 6
    Yes, `stringstream ss;` implies the use of the horrible `using namespace std;` or the only slightly less horrible `using std::stringstream;` directives. Do yourself a favor and just go with `std::stringstream ss;`. –  Dec 02 '13 at 20:10
  • For the `stringstream` they probably have at some point a `using namespace std`. The main difference between using `std::stringstream` and `stringstream` would be in the case theres also something else called `stringstream` (not from std). In the second case, the compiler will give an error. – wendelbsilva Dec 02 '13 at 20:11

3 Answers3

5

The difference is that unless you've done something abominable like using namespace std; the former will compile and the latter won't.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • 1
    "abominable" -- Sir, you just taught me a new word today. –  Dec 02 '13 at 20:13
  • Why is `using namespace std;` considered bad practice? –  Dec 02 '13 at 20:14
  • 3
    @RAF1940 http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice Verbatim =) – Captain Giraffe Dec 02 '13 at 20:15
  • Interesting. Thanks for the link! –  Dec 02 '13 at 20:16
  • 4
    @RAF1940: Long story, too long for a comment, but suffice to say that namespaces are created to create a division among names, and `using namespace` breaks that division by modifying lookup and potentially cause ambiguities and harder to read (interpret) code. The using directive does what it promises, but not everyone is aware of what that promise is. Lookup is complex enough without it :) – David Rodríguez - dribeas Dec 02 '13 at 20:16
  • 3
    @RAF1940 - there are many questions on SO about compile-time errors that result from ambiguities because of `using namespace std;`. The primary purpose of namespaces is to make it easier avoid ambiguities; `using namespace std;` in effect says "damn the torpedoes, full speed ahead!" – Pete Becker Dec 02 '13 at 20:16
  • 2
    @CaptainGiraffe I find that the maintainability aspect of verbose namespacing is more valuable. If I come across something like `boost::somelib::obscureclass::obscureiterator` I can at least follow the chain to find documentation, but if I just see `obscureclass::obscureiterator` I have to dig for it. – Sam Cristall Dec 02 '13 at 20:20
  • 1
    In my experience, `using namespace` should be restricted to only the most trivial academic exercises. It has no place in a professional -quality codebase as it *effectively* combines the given namespace with the global namespace, which can lead to a massive number of naming conflicts and bugs that are very difficult to track down. – Zac Howland Dec 02 '13 at 20:22
2

The difference is that using just stringstream implies that you have using namespace std; or using std::stringstream. Without one of those, the code without the fully qualified name will not compile.

The using namespace std is usually considered a bad practice, because it removes all benefits that you get from C++ namespaces - namely, name clashes. If you use the using directive, especially with STD, which has classes such as list, iterator, etc, you open yourself to the possibility for name clashes with your own classes.

This can in the best case result in weird compile time errors, or in the worst case, silent substitution of the functions that you call, with hard to trace problems.

divinas
  • 1,787
  • 12
  • 12
0

std::stringstream refers to one of many objects found in the standard library, stringstream. If we think of libraries as physical library buildings, then your program has to go and "get the book" that is stringstream to use it. This is what the std:: syntax does, it indicates to your program that it has to go to the standard library to get whatever is on the right-side of ::. This works for many libraries, such as boost:: or even sub-namespaces like std::tr1::. (Note that technically namespaces are not equal to libraries, but we'll ignore that for this)

Now, by writing simple stringstream without the std:: it implies that somewhere using namespace std; has been written. Using our previous analogy, using namespace std; basically mashes all of those library books into your program. You don't need to get them anymore, but your program is now cluttered with every book from it. Naturally, this can cause problems if you do it with several libraries since now a book from std:: might get mixed up with a book from boost::.

Sam Cristall
  • 4,328
  • 17
  • 29