Home > Backend Development > C++ > When are C Class Template Member Functions ODR-Used?

When are C Class Template Member Functions ODR-Used?

Barbara Streisand
Release: 2024-12-07 13:24:13
Original
916 people have browsed it

When are C   Class Template Member Functions ODR-Used?

Understanding ODR-Use in C

In the context of class templates, the term "odr-use" has been encountered. This concept plays a crucial role in understanding the instantiation of member functions within class templates.

According to the C standard [basic.def.odr], a variable or function is odr-used if it is used in a context that requires an actual definition rather than a mere declaration. This includes cases where the variable or function is directly referenced or potentially evaluated within an expression. However, an exception exists for objects that can participate in constant expressions and for which the lvalue-to-rvalue conversion is immediately applied.

For class template member functions, the definition becomes more specific: a non-overloaded function (member or otherwise) that is referenced in a potentially-evaluated expression or is a part of an overload candidate set is odr-used unless it is a pure virtual function and its name is not explicitly qualified.

This definition may raise questions about how the ODR-use rule applies across multiple compilation units. To clarify, a class template is instantiated only if it is needed for ODR-use within at least one compilation unit. Simply instantiating a class template does not automatically instantiate all its member functions unless they are explicitly odr-used.

In summary, ODR-use is a concept defined in the C standard to determine which entities require an actual definition. It plays an important role in template instantiation, with member functions only being instantiated if they are odr-used within a compilation unit.

The above is the detailed content of When are C Class Template Member Functions ODR-Used?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template