C++中的标识字符串流

Identity string stream in C++

本文关键字:字符串 标识 C++      更新时间:2023-10-16

有可能有一个具有这种行为的字符串流吗?

std::istringstream stringstream("hello world");
std::string output;
stringstream >> output;
assert(output == "hello world");

用例:

template <typename T>
T as(void) {
    T t;
    std::istringstream stringstream(this->m_str);
    stringstream >> t;
    return t;
}

如果T是std::string,它将不起作用。我可以进行专业化,但我想避免它。

不直接,因为接受std::stringoperator>>重载将只读取到第一个空白,而不包括第一个空白空间,因此调用将只获得"hello"