以下代码在GCC,Clang和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
}
我认为直接初始化的唯一情况是复制初始化不起作用的时候构造函数是显式的,在这种情况下不是这样。发生了什么事?
答案 0 :(得分:6)
该构造函数 标记为显式,因此只能与直接初始化一起使用。 §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);
(basic_stringbuf
,basic_istringstream
和basic_ostringstream
也是如此。)