简体   繁体   English

从std :: stringstream传递std :: string引用作为参数

[英]Passing std::string reference from std::stringstream as parameter

I am using std::stringstream to construct a string and then attempting to pass the completed string as a reference to a function which takes as a parameter a std::string&. 我使用std :: stringstream构造一个字符串,然后尝试将完成的字符串作为对函数的引用传递给一个函数,该函数将std :: string&作为参数。

I am getting a compilation error on GCC: 我在GCC上收到编译错误:

../src/so.cpp:22:21: error: invalid initialization of non-const reference of type ‘std::string& {aka std::basic_string<char>&}’ from an rvalue of type ‘std::basic_stringstream<char>::__string_type {aka std::basic_string<char>}’
../src/so.cpp:12:6: error: in passing argument 1 of ‘void myFunc(std::string&)’
make: *** [src/so.o] Error 1

The same code is compiling on Windows VS2012, but fails on my Linux and Android builds. 在Windows VS2012上编译相同的代码,但在我的Linux和Android版本上失败。 What is the reason for this? 这是什么原因?

I can work around the issue by temporarily assigning ss.str() to a temporary std::string and then passing that string by reference, but this seems slightly silly. 我可以通过暂时将ss.str()分配给临时的std :: string然后通过引用传递该字符串来解决这个问题,但这看起来有些愚蠢。 What is the correct way to do this cleanly? 干净利落的正确方法是什么?

#include <iostream>
#include <sstream>

void myFunc (std::string& msg)
{
    std::cout << msg << std::endl;
}

int main (void)
{
    std::stringstream ss;
    ss << "this is a test";

    myFunc (ss.str());              // Fails

    std::string s = ss.str();
    myFunc (s);                     // Pass

    return 0;
}

The issue is that myFunc takes a non-const lvalue reference. 问题是myFunc采用非const左值引用。 stringstream::str() returns a string by value. stringstream::str()按值返回一个字符串。 You cannot bind a temporary to a non-const lvalue reference in standard C++, but VS has an "extension" that allows this. 您不能在标准C ++中将临时绑定到非const左值引用,但VS有一个允许此扩展的“扩展”。 That is the reason it compiles on VS and not on other compilers. 这就是它编译VS而不是其他编译器的原因。

const lvalue references, on the other hand, can bind to rvalues. 另一方面, const左值引用可以绑定到rvalues。 So modifying your function thus would make it work: 所以修改你的功能会使它工作:

void myFunc (const std::string &msg) { /* as before */ }

Change this: 改变这个:

void myFunc (std::string& msg)

to this: 对此:

void myFunc (const std::string& msg)
//           ^^^^^ this will allow temporaries like ss.str()

There are versions of Visual Studio that will foolishly allow a temporary to bind to a non-const reference. 有些版本的Visual Studio会愚蠢地允许临时绑定到非const引用。 It is dangerous and not valid C++ , however. 然而,这是危险的,无效的C ++

Since you're not writing to the string inside myFunc , accept a const reference: 因为你没有写入myFunc的字符串,所以接受一个const引用:

void myFunc (std::string const &msg)
{
  std::cout << msg << std::endl;
}

Those can be bound to temporary objects 那些可以绑定到临时对象

So the reason that you are having this error, is that ss.str() returns a const string, not a string. 所以你遇到这个错误的原因是ss.str()返回一个const字符串,而不是一个字符串。 By creating a new string you are creating a non-const variable that is set to the same value as ss.str() and thus can be passed into myFunc(). 通过创建一个新字符串,您将创建一个非const变量,该变量设置为与ss.str()相同的值,因此可以传递给myFunc()。 Making a new string as you have it is probably the easiest way to fix this and still use the function as is. 创建一个新的字符串可能是解决这个问题的最简单方法,并且仍然按原样使用该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM