I have three function calls that I think should be treated (about) the same, but clearly they are not. I'm trying to understand why one of three doesn't compile (g++ -std=c++0x).
// Minimal example to reproduce a compile bug I want to understand.
#include <iostream>
#include <string>
using namespace std;
void bar(const string &&x) { cout << "bar: " << x << endl; }
string returns_a_string() { return string("cow"); }
int main( int argc, char *argv[] )
{
bar(string("horse")); // ok
bar(returns_a_string()); // ok
string aardvark = "aardvark";
bar(aardvark); // not ok, fails to compile, error in next comment
/*
rvalue-min.cpp:29:22: error: cannot bind ‘std::string {aka std::basic_string<char>}’ lvalue to ‘const string&& {aka const std::basic_string<char>&&}’
rvalue-min.cpp:10:6: error: initializing argument 1 of ‘void barR(const string&&)’
*/
}
This question is a bit along the lines of C++0x rvalue references - lvalues-rvalue binding, but, if it's answered there, my apologies, I wasn't able to distil it out.
What I want is to be able to call my function bar() with any sort of string and have it just work. It's enough to define void barR(const string &x)
, but I'd really like to understand why.
Thanks much for any help in understanding why the third call is different.
bar(std::string &&)
. – Germann