4

The following code fails in GCC, Clang and Visual Studio:

#include <string>
#include <sstream>

int main() {
    std::string s = "hello"; // ok, copy-initialization
    std::stringstream ss1(s); // ok, direct-initialization
    std::stringstream ss2 = s; // error
}

I thought the only case where direct-initialization works while copy-initialization doesn't is when the constructor is explicit, which it is not in this case. What's going on?

Nikolai
  • 3,053
  • 3
  • 24
  • 33
  • 1
    Codepad example: http://codepad.org/du90Fkck – Brendan Long Oct 15 '13 at 22:36
  • I'm confused with why you're trying to set a std::stringstream to a string. There is no default assignment operator for this. Take a look [here](http://www.cplusplus.com/reference/sstream/stringstream/operator=/). – Dan Oct 15 '13 at 22:42
  • 2
    @Dan that's initialization, not assignment. – jrok Oct 15 '13 at 22:52

1 Answers1

7

That constructor is marked explicit, so can only be used with direct-initialization. §27.8.5:

explicit basic_stringstream(
ios_base::openmode which = ios_base::out | ios_base::in);

explicit basic_stringstream(
const basic_string<charT,traits,Allocator>& str,
ios_base::openmode which = ios_base::out | ios_base::in);

basic_stringstream(const basic_stringstream& rhs) = delete;

basic_stringstream(basic_stringstream&& rhs);

(The same is true for basic_stringbuf, basic_istringstream, and basic_ostringstream.)

GManNickG
  • 494,350
  • 52
  • 494
  • 543