Home > Article > Backend Development > Is it legal to pass a C object into its own constructor?
Is It Legal to Pass a C Object into Its Own Constructor?
It is perplexing that code like this appears to function:
#include <iostream> struct Foo { Foo(Foo& bar) { std::cout << &bar << std::endl; } }; int main(int argc, char** argv) { Foo foo(foo); // I can't believe this works... std::cout << &foo << std::endl; // but it does... return 0; }
By passing an uninitialized object's address into its own constructor, we appear to be establishing a circular definition. The question arises: do the standards permit passing an object to a function before its construction? Or does this constitute undefined behavior?
Legal, But Not Uncommon
Passing an uninitialized object's address into its own constructor is not undefined behavior. While foo is uninitialized, we use it in a standard-approved manner.
Object memory allocation occurs before complete initialization. Consequently, binding a reference to such a variable and retrieving its address are permitted during this interval.
This behavior aligns with Defect Report 363, which clarifies that references created in such a situation are considered valid. Section 3.8 [basic.life] of the C 14 standard further specifies that using glvalues that reference allocated but uninitialized objects is admissible within limits.
Namely, we cannot apply lvalue-to-rvalue conversions, access non-static data members or invoke non-static member functions, bind to virtual base class references, or employ dynamic_cast or typeid.
In our example, we avoid these forbidden actions, binding a reference and acquiring the address.
Compiler Warnings
Despite the behavior's legitimacy, Clang issues a warning:
warning: variable 'foo' is uninitialized when used within its own initialization [-Wuninitialized]
This warning is justified since the generation of an indeterminate value from an uninitialized automatic variable constitutes undefined behavior. Nonetheless, our use does not yield an indeterminate value and is therefore legal.
Further Considerations
While self-initialization through passing an object to its own constructor is not inherently beneficial or advisable, it can be an intriguing method for exploring class behavior. However, self-initialization through assignment, as in int x = x;, does constitute undefined behavior.
The above is the detailed content of Is it legal to pass a C object into its own constructor?. For more information, please follow other related articles on the PHP Chinese website!