11

Possible Duplicate:
Global const string& smells bad to me, is it truly safe?

I've stumbled across the following code and wondered of its merits

std::string const & thestring( "XYZ" );

It's the fact that it's constructing the object and referring to it by a reference. I'm used to seeing

std::string const theString( "XYZ" );

and was wondering what the difference was. I'm reasonably happy that the object wouldn't be destructed early as the object lives on the stack along with the reference.

Community
  • 1
  • 1
ScaryAardvark
  • 2,855
  • 4
  • 30
  • 43
  • Never seen case 1, it would seem it's pointless. Construct a temporary and bind a const reference to it ( thereby extending the lifetime to the references scope ). The second case is 1 degree less indirection, and would for all intents purposes act as case one. But perhaps I'm missing something not obvious. – Ylisar Mar 08 '12 at 10:15
  • 2
    This looks related: http://stackoverflow.com/a/1028511/390913 – perreal Mar 08 '12 at 10:16
  • The only difference I can think of is that the non reference one was declared as const, and thus const casting away the constness is UB – PlasmaHH Mar 08 '12 at 10:19
  • @PlasmaHH: interesting difference, I wonder what the Standard has to say about the const-ness of a bound temporary. – Matthieu M. Mar 08 '12 at 10:21
  • @MatthieuM.: I don't think the question is about the safeness, but rather about what advantage one possibly has over the other. – PlasmaHH Mar 08 '12 at 10:22
  • @MatthieuM.: Since a temporary can also be bound to an rvalue reference, I dont think it has the "declared as const, thus const casting is UB" property. – PlasmaHH Mar 08 '12 at 10:24
  • @PlasmaHH casting away constness is not UB. – J.N. Mar 08 '12 at 10:27
  • Perhaps it's a typo that the original programmer didn't notice because it compiles & works the same as the non-reference version – Rob Agar Mar 08 '12 at 10:28
  • @PlasmaHH Matthieu This used to be an open issue, if I remember correctly. `const int& x = 3` if a temporary is considered non-const modifying it through a `const_cast`ed x would be safe but this would prevent the compiler from storing it as data. – pmr Mar 08 '12 at 10:28
  • @PlasmaHH: `Since a temporary can also be bound to an rvalue reference, I dont think it has the "declared as const, thus const casting is UB" property` Not correct in this case, and [previously discussed](http://stackoverflow.com/questions/8245027/am-i-right-in-saying-that-const-cast-followed-by-modification-on-a-ref-to-const) (you were there!). – Lightness Races in Orbit Mar 08 '12 at 10:32
  • @pmr: do you remember the settlement ? Ie was it decided that it was safe (and not data) or unsafe ? – Matthieu M. Mar 08 '12 at 10:32

1 Answers1

0

depends completely on what's happening with the string where the function is located. It needs to be save to be refered.

if you have a variable in the function that lives in, lets say the object which the function belongs to.

class yourClass {
    string a; 

    string const & yourFunction(string b) {
        a = b;
        return a;
    }
}

Also, not too sure what would happen if you would just return "somestring" directy, but when you do a similar thing with inparameters it works like this:

void test(const string& value);

If you then would pass "someString" it would not be handled as a reference, just a value. If you remove the "const" you would get trouble and would not be able to pass "someString" as a value.

chikuba
  • 4,229
  • 6
  • 43
  • 75
  • I think that your understand of const references is a bit fuzzy (or perhaps not grealy worded). A temporary may be bound to a `const&` variable or function parameter, in this case the lifetime of the temporary is extended to that of the reference. However this does not work with return values or class attributes, so you need a named object there. – Matthieu M. Mar 08 '12 at 10:39
  • ofc i can be bound to a const&, just ment that you can pass a plain string as inparameter when you define it as const& but not just &.. correct me if im wrong. and what do you mean about the last thing? not sure if i understand what you mean there – chikuba Mar 08 '12 at 10:54
  • I meant that if you use an attribute that is a `std::string const&` in a class, you cannot bind a temporary string to it. – Matthieu M. Mar 08 '12 at 11:45
  • not when returning a value no. think we are on the same page here. cant really see where I missunderstood something. might just be my bad english – chikuba Mar 08 '12 at 23:12