相关疑难解决方法(0)

将临时对象转换为非常量引用时出错

这是一个可重现的示例,取自有关使用临时 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 时,一切都可以正常编译。

似乎从 …

c++ clang++

6
推荐指数
1
解决办法
215
查看次数

使用匿名字符串流来构造字符串

我想直接读入一个代码如下的字符串:

std::string myString(( std::ostringstream() << myInt << " "
                                            << myFloat << " "
                                            << std::boolalpha << myBool ).str());
Run Code Online (Sandbox Code Playgroud)

但VS2012给了我一个basic_ostream没有str()方法的投诉.

有没有办法用匿名字符串流来做到这一点?

c++ string sstream

5
推荐指数
2
解决办法
1950
查看次数

标签 统计

c++ ×2

clang++ ×1

sstream ×1

string ×1