Returns a temporary object by reference

Is it possible to return a link from a function similar in this code example:

string &erase_whitespace(string &text)
{
    text.erase(**etc.**);
    return text;
}

Call:

string text = erase_whitespace(string("this is a test"));
cout << test;

Does this code work? In Visual C ++, it does not crash, but it looks wrong.

thanks

+3
source share
10 answers

From section 12.2.3 of the C ++ 2003 standard (draft)

Temporary objects are destroyed as the last step in evaluating the full expression (1.9), which (lexically) contains the point at which they were created.

ยง 12.2.4:

There are two contexts in which temporary objects are destroyed at another point, the expression ....

ยง 12.2.5:

- . , , , , , .... (5.2.2) , .

ยง8.5.3.5 - , , const-. , , , ( ). :

class Foo {
    ...
    operator Bar&() const;
...
void baz(Bar &b);
...
    baz(Foo()); // valid
    baz(Bar()); // not valid

- ยง 12.3.2.1, : " [...] [...] ( ) ". , , Bar .

- (ยง 5.17), (ยง 1.9.12) . ( , , , ):

  • string& text erase_whitespace
  • erase_whitespace .
  • erase_whitespace
  • string text
  • .

, . , , erase_whitespace . , , , , , , , , . - , ...

+9

Visual C, :

string erase_whitespace (const string &input)
{
  string output = input.erase (...);
  return output;
}

, , (.. , ). , , , , .

+7

.

-, , , :

struct myclass
{
    myclass& foo()
    { cout << "myclass::foo()"; return *this }
    myclass& bar()
    { cout << "myclass::bar()"; return *this }
};
...
myclass obj;
obj.foo().bar();

-, ++ ( SO, ):

// passing string("this is a test") is wrong
string text = erase_whitespace(string("this is a test"));

, (, VC) , . , .

+4

, , -const rvalues. , , . .

: , . :

string const& dangling_reference =
    erase_whitespace(string("this is a test"));

, . , - ++ ( ).

, . , , .

, , :

string erase_whitespace(string text)
{
    text.erase(**etc.**);
    string ret; ret.swap(text);
    return ret;
}

, , . , , rvalue. , NRVO ( ). ,

string foo = erase_whithespace("  blah  ");

ctor std::string. , NRVO .

+3

, , , , . , . , :

std::string text("this is a test");
erase_whitespace(text);
cout << test;

, , std::string, , std::string , . , std::string , ++, .

, , .

, , , , , - , Skizz ( ++, Visual C):

std::string erase_whitespace (const string &input)
{
    std::string output(input);
    output.erase (...);
    return output;
}

, const char* , ++ std::string, std::string , const char*. , . , , , , .

+2

, , w/pointer:

string* erase_whitespace(string* text)
{
    text->erase(**etc.**);
    return text;
}

string* text = erase_whitespace(new string("this is a test"));
cout << *text;
delete text;
0

, const-reference. V++ nonconst-reference.

0

.

text = erase_whitespace ( ( " " ))

:

string temp ( " " );

string text = erase_whitespace (temp);

0

, , , string - . , , .

0

, , , DOES . , . :

string& text = erase_whitespace(string("this is a test"));

V++, "undefined". , - , , , , .

, V++.

Skizz , , .

0

Source: https://habr.com/ru/post/1722569/


All Articles