4

以下代码在 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
}

我认为直接初始化有效而复制初始化无效的唯一情况是构造函数是显式的,在这种情况下不是。这是怎么回事?

4

1 回答 1

7

该构造函数标记为显式,因此只能用于直接初始化。§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_istringstreambasic_ostringstream

于 2013-10-15T22:35:36.197 回答