Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

c++ - Is it well defined to reference a variable before it's constructed

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The behaviour seems to be undefined in C++20. The change was made by P1358, resolving CWG 2256. As defect resolutions are generally retroactive, this code should be considered UB in all versions of C++.

According to [basic.life]/1:

... The lifetime of an object of type T begins when:

  • storage with the proper alignment and size for type T is obtained, and
  • its initialization (if any) is complete (including vacuous initialization) ...

At the time when f(a) is called, the object a has not yet begun its lifetime since its initialization has not completed. According to [basic.life]/7:

Similarly, before the lifetime of an object has started but after the storage which the object will occupy has been allocated ... any glvalue that refers to the original object may be used but only in limited ways. ... The program has undefined behavior if:

  • the glvalue is used to access the object ...

Thus, writing to a before its initialization has completed is UB, even though the storage has already been allocated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...