1

Topic: Whether correctly to return the link to a function parameter

It is cast by the previous topic. Whether correctly to return the link on ** referential ** a function parameter?
Here so:

int const &foo (int const &a)
{
return a;
}
int const &f = foo (3);

As a matter of fact we return the link to temporary variable. But in this case the constant.
It is all looks as a bug, but any compiler on my storage did not shout .

2

Re: Whether correctly to return the link to a function parameter

But, in general, it can be and correct:

struct Foo
{
Foo &operator= (int);
};
Foo &convert (Foo &a)
{
return a;
}
Foo f;
convert (f) = 6;

3

Re: Whether correctly to return the link to a function parameter

petrav wrote:

It is cast by the previous topic. Whether correctly to return the link on ** referential ** a function parameter?
Here so:

int const &foo (int const &a)
{
return a;
}
int const &f = foo (3);

to Return correctly, and to use f - is not present.

petrav wrote:

As a matter of fact we return the link to temporary variable.

In the given code - yes.

petrav wrote:

But in this case the constant.

And it is what role plays?

petrav wrote:

It is all looks as a bug, but any compiler on my storage did not shout .

And?

4

Re: Whether correctly to return the link to a function parameter

wrote:

the compiler now the clever went

And its using?
What, for example, it is possible to tell about it?
(By the way, there and  is. Only .)

5

Re: Whether correctly to return the link to a function parameter

wrote:

to Return correctly, and to use f - is not present.

Why it is not correct? Link holding so it seems is called? It is correct:

std:: string getStr ();
std:: string &myStr = getStr ();
wrote:

And it is what role plays?
AND?

Whether yes the role poorly what there is played. It is played, yes-yes. Put here not in OOP and not in . We simply argue on dirty tricks With ++. On purpose to raise the literacy.

6

Re: Whether correctly to return the link to a function parameter

, and what the compiler made not so?

7

Re: Whether correctly to return the link to a function parameter

wrote:

petrav, the compiler now the clever went . At foo parameter the link. Means should be where that variable (constant) in storage on which this link works.

Well it is possible. But I do not know the assembler. Though learned it... At school. Forgive, but your link of anything for me did not tell.

8

Re: Whether correctly to return the link to a function parameter

petrav wrote:

As a matter of fact we return the link to temporary variable.

You found temporary variable in what place of this code?

9

Re: Whether correctly to return the link to a function parameter

petrav wrote:

link Holding so it seems is called?

Only it here does not work.

http://eel.is/c draft/class.temporary#6.9 wrote:

A temporary object bound to a reference parameter in a function call persists until the completion of the full-expression containing the call.

10

Re: Whether correctly to return the link to a function parameter

Dimitry Sibiryakov wrote:

it is passed...
You found temporary variable in what place of this code?

Here we create on a stack type temporary variable int with value 3:

foo (3);

Also we transfer this variable in function foo ().

11

Re: Whether correctly to return the link to a function parameter

, there UB so should not.

12

Re: Whether correctly to return the link to a function parameter

wrote:

there is not present UB.

Mere allegation.

13

Re: Whether correctly to return the link to a function parameter

wrote:

it is passed...
Only it here does not work.
it is passed...

Just worked in Studio 2015. If it is fair, I did not understand your citation on English If you explain... Would be abruptly. But I assume that there is written: temporary variables exist while expression is fulfilled, and expression comes to an end with a semicolon.

#include <string>
std:: string getStr (std:: string const &a_str)
{
return a_str;
}
int main (int argc, char *argv [])
{
std:: string &s = getStr ("123");
}

14

Re: Whether correctly to return the link to a function parameter

petrav wrote:

Just worked in Studio 2015.

I wrote about a situation from the Op-post. At what here getStr?

petrav wrote:

If it is fair, I did not understand your citation on English

It does not help?

15

Re: Whether correctly to return the link to a function parameter

wrote:

it is passed...
You contradict yourself.

the Second mere allegation.

16

Re: Whether correctly to return the link to a function parameter

So did not work:

#include <string>
std:: string const &getStr (std:: string const &a_str)
{
return a_str;
}
int main (int argc, char *argv [])
{
std:: string const &s = getStr ("123");
}

The temporary variable finished the life after a semicolon. But thus variable contents s the correct. Blank line. Not clearly absolutely.

17

Re: Whether correctly to return the link to a function parameter

petrav wrote:

Temporary variable

That such "temporary variable"? I know about transient objects.

18

Re: Whether correctly to return the link to a function parameter

wrote:

it is passed...
I wrote about a situation from the Op-post. At what here getStr?
it is passed...
It does not help?

I got it. Thanks.

19

Re: Whether correctly to return the link to a function parameter

petrav wrote:

link Holding so it seems is called?

it is fine, without citations from the standard (as that citation not absolutely expresses that is necessary).
Uproshchyonno telling, transient object lifetime lasts before link lifetime, to which it .
When lifetime of function parameters terminates?

20

Re: Whether correctly to return the link to a function parameter

wrote:

it is passed...
What is "temporary variable"? I know about transient objects.

Objects also are variables. According to the decision of Straustrupa With ++ was developed so, whenever possible, to erase a difference between... However, it seems to me we argue on terms. A way there will be objects.

21

Re: Whether correctly to return the link to a function parameter

petrav wrote:

Objects also are variables.

To me from their descriptions (http://eel.is/c draft/intro.object#def:object, http://eel.is/c draft/basic#def:variable) it is not obvious.

22

Re: Whether correctly to return the link to a function parameter

wrote:

it is passed...
All right, without citations from the standard (as that citation not absolutely expresses that is necessary).
Uproshchyonno telling, transient object lifetime lasts before link lifetime, to which it .
When lifetime of function parameters terminates?

Time of their life comes to an end when we quit function. Yes I understood. Accordingly if we return the link to transient object generated for a function parameter, with all aggravating...
It is necessary to understand why in my last example a line in a correct state.   the pointer, length of a line and the reserved size?

23

Re: Whether correctly to return the link to a function parameter

petrav wrote:

Here we create on a stack type temporary variable int with value 3:

We do not create it on a stack. Here there is a constant in a code segment, the link on which and
It is transferred in function. Without optimization GCC does not produce warnings and the program returns
3. With optimization it returns 0 and the compiler fairly warning

aaa.cpp:11:24: warning: ' <anonymous> ' is used uninitialized in this function
[-Wuninitialized]
printf ("f = %d\n", f);
^

24

Re: Whether correctly to return the link to a function parameter

The flat;
Thanks. I understood all. I blunted something, after all all was obvious. But it becomes obviously frequent when with a nose stick.
Not clearly only why object s the empty. But it, probably, not very well.

25

Re: Whether correctly to return the link to a function parameter

petrav wrote:

Time of their life comes to an end when we quit function.

Aha. So from this point of view that citation from the standard prolongs lifetime of transient object before the termination of calculation of the full expression.
However, f this citation is useful to the answer to a question on a correctness of usage of a variable in itself.

petrav wrote:

It is necessary to understand why in my last example a line in a correct state.

I do not know that such "a correct state" for the died objects.