This is similar in spirit to a question that was asked and answered for c
. The comments there implied that a precise answer would be different for c++
, so here is a similar question for code written in c++
.
Is the following program well defined?
int f(int& b)
{
b = 42;
return b;
}
int a { f(a) };
It seems all right to me, but on the other hand, how is a
being constructed from a value that is computed by a function, that itself modifies a
? I'm having a chicken-and-egg feeling about this, so an explanation would be nice. For what it's worth, it appears to work.
This seems to be the same question, so here goes; would the answer be different for class types and fundamental types. i.e. Is the following well formed?
struct S { int i; };
S f(S& b)
{
b.i = 42;
return b;
}
S a { f(a) };
Again, for what it's worth, this appears to work as well.
See Question&Answers more detail:os