这是一个可重现的示例,取自有关使用临时 stringstream 对象的问题:
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
std::string transform(std::string);
int main()
{
int i{};
cout << transform( static_cast<stringstream &>(stringstream() << i).str() );
}
Run Code Online (Sandbox Code Playgroud)
在 MacOS High Sierra 下尝试使用clang 9.0.0 版进行编译时,出现以下错误:
$ clang++ -std=c++11 x.cc -c
x.cc:12:24: error: non-const lvalue reference to type 'basic_stringstream<...>' cannot bind to a temporary of type 'basic_stringstream<...>'
cout << transform( static_cast<stringstream &>(stringstream() << i).str() );
^ ~~~~~~~~~~~~~~~~~~~
1 error generated.
Run Code Online (Sandbox Code Playgroud)
当在同一台机器上(也在 Linux 上)使用 g++ 9.2.0 时,一切都可以正常编译。
似乎从 …
我想直接读入一个代码如下的字符串:
std::string myString(( std::ostringstream() << myInt << " "
<< myFloat << " "
<< std::boolalpha << myBool ).str());
Run Code Online (Sandbox Code Playgroud)
但VS2012给了我一个basic_ostream没有str()方法的投诉.
有没有办法用匿名字符串流来做到这一点?