lvalue to rvalue in function return type

Hey,

i am currently thinking about the following problem and cant find any more goolge helps:
i need to demonstrate that functions returning primitive types return rvalues and functions returning objects return lvalues. easy enough with an int and a string object. the part i am stuck on is: how can i change the lvalue i get from a function, say a string, to a rvalue. already in the return type. such that something like the stupid code:
 
lvalFunc() = "Hello World\n";

does not compile anymore. i can clearly change the return type to a const, but that simply prevents the returned string from being changed and does not make it an lvalue, right?!
I d be greatful for any ideas (:
Last edited on
i need to demonstrate that functions returning primitive types return rvalues and functions returning objects return lvalues

This can't be demonstrated because this is false: value category of a function call expression is not related to whether the return type is a class type or a scalar:

1
2
3
4
5
6
7
8
9
int f1();
std::string f2();
int& f3();
std::string& f4();

f1(); // rvalue
f2(); // rvalue
f3(); // lvalue
f4(); // lvalue 

Last edited on
hm, i am not into c++ enough yet to know whether i just misformulated or i do not 100% understand your point - if that makes any sense :>
1
2
3
std::string f1();

f1() = "Hello World\n";0


compiles just fine for me. doesnt it need to be an lvalue for that assignment to work? i am a little confused here (:
C++ allows member functions (const and non-const) to be called on rvalues. Member functions can make changes to the object. That is exactly what happens here because operator= is a member function.
doesnt it need to be an lvalue for that assignment to work?

No, but it does need to be an lvalue for the address-of operator to work:

1
2
std::string f1();
std::string* p = &f1();

My compiler output:
"test.cc", line 8.25: 1540-0212 (S) The operand of the "&" operator must be an lvalue.


To answer the title question, to convert from lvalue to rvalue, you can use static_cast:

1
2
3
std::string& f3();
std::string* p1 = &f3(); //  ok
std::string* p2 = &static_cast<std::string>(f3()); //  same error 

1
2
std::string f1();
std::string* p = &f1();

does compile for me. dont know what i am doing wrong. any other operators that i can use on strings that require an lvalue?
1
2
std::string f1();
std::string* p = &f1();

does compile for me. dont know what i am doing wrong.

You're using a compiler which offers this as a non-standard language extension, apparently (I can't imagine what this compiles to, though!).

You could try GCC (in standards-compliant mode), although its error message is a bit less clear: http://liveworkspace.org/code/c8bafc461e3596056da5925a535a594e

any other operators that i can use on strings that require an lvalue?

It's tricky because lvalue-to-rvalue conversion is implicit, so if an operator accepts rvalue arguments, it will accept lvalue arguments as well. With sufficient C++11 support, you could write a little tester class, but I can't think of another operator.
Last edited on
Topic archived. No new replies allowed.