


## Does Binding a Const Reference to a Temporary Object\'s Sub-part Extend its Lifetime?
Can a const reference to a temporary object's sub-part extend the object's lifetime?
Consider the following code snippet:
<code class="c++">#include <stdio.h> struct P2d { double x, y; P2d(double x, double y) : x(x), y(y) {} ~P2d() { printf("Destructor called\n"); } }; P2d center() { return P2d(10, 10); } int main(int argc, const char *argv[]) { const double& x = center().x; printf("x = %.18g\n", x); return 0; }</stdio.h></code>
When compiled with GCC 5.2.0, the P2d temporary instance is destroyed before entering the printf call in main. Despite this, the value of x is preserved and printed correctly. In other words, instead of binding x directly to the P2d temporary's x member, a second temporary double is created to copy the member's value.
On the other hand, Clang extends the lifetime of the P2d temporary to the lifetime of the x reference, resulting in the destructor being called after the printf in main.
This raises the question: is this behavior a bug in GCC or allowed by the standard?
Analysis
CWG 1651 addresses this issue:
The resolution of issues 616 and 1213, making the result of a member access or subscript expression applied to a prvalue an xvalue, means that binding a reference to such a subobject of a temporary does not extend the temporary's lifetime. [class.temporary] should be revised to ensure that it does.
Currently, [class.temporary]/5 states: "The second context is when a reference is bound to a temporary." This has been interpreted to apply only to references that bind directly to temporary objects. Therefore, in the code snippet above, center().x is treated as a prvalue (by both Clang and GCC), and [class.temporary]/5 is not applicable.
However, GCC and Clang have not yet implemented the resolution of DR 1651, which is N3918. This resolution explicitly states that "member access and subscript expressions applied to a temporary expression yield temporary expressions," and adds that "the corresponding temporary object (if any) persists for the lifetime of the reference."
Thus, based on the wording of N3918, it appears that Clang is implementing the intended behavior, while GCC is not. DR 60297 notes that GCC does not extend lifetime for scalar subobjects of temporaries, as they are not covered by [dcl.init.ref]/(5.2.1.1).
In summary, the current behavior in GCC is correct according to the current wording of the standard, but the standard is likely to be revised to require lifetime extension in this case. Clang is already implementing the expected future behavior.
The above is the detailed content of ## Does Binding a Const Reference to a Temporary Object\'s Sub-part Extend its Lifetime?. For more information, please follow other related articles on the PHP Chinese website!

The main differences between C# and C are syntax, memory management and performance: 1) C# syntax is modern, supports lambda and LINQ, and C retains C features and supports templates. 2) C# automatically manages memory, C needs to be managed manually. 3) C performance is better than C#, but C# performance is also being optimized.

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.

C still dominates performance optimization because its low-level memory management and efficient execution capabilities make it indispensable in game development, financial transaction systems and embedded systems. Specifically, it is manifested as: 1) In game development, C's low-level memory management and efficient execution capabilities make it the preferred language for game engine development; 2) In financial transaction systems, C's performance advantages ensure extremely low latency and high throughput; 3) In embedded systems, C's low-level memory management and efficient execution capabilities make it very popular in resource-constrained environments.

The choice of C XML framework should be based on project requirements. 1) TinyXML is suitable for resource-constrained environments, 2) pugixml is suitable for high-performance requirements, 3) Xerces-C supports complex XMLSchema verification, and performance, ease of use and licenses must be considered when choosing.

C# is suitable for projects that require development efficiency and type safety, while C is suitable for projects that require high performance and hardware control. 1) C# provides garbage collection and LINQ, suitable for enterprise applications and Windows development. 2)C is known for its high performance and underlying control, and is widely used in gaming and system programming.

C code optimization can be achieved through the following strategies: 1. Manually manage memory for optimization use; 2. Write code that complies with compiler optimization rules; 3. Select appropriate algorithms and data structures; 4. Use inline functions to reduce call overhead; 5. Apply template metaprogramming to optimize at compile time; 6. Avoid unnecessary copying, use moving semantics and reference parameters; 7. Use const correctly to help compiler optimization; 8. Select appropriate data structures, such as std::vector.

The volatile keyword in C is used to inform the compiler that the value of the variable may be changed outside of code control and therefore cannot be optimized. 1) It is often used to read variables that may be modified by hardware or interrupt service programs, such as sensor state. 2) Volatile cannot guarantee multi-thread safety, and should use mutex locks or atomic operations. 3) Using volatile may cause performance slight to decrease, but ensure program correctness.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor
