Home >Backend Development >C++ >What Constitutes an ODR-Use in C ?

What Constitutes an ODR-Use in C ?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-08 11:39:12770browse

What Constitutes an ODR-Use in C  ?

What is "odr-use"?

In the context of C , "odr-use" refers to the situations where an entity's definition must be provided during compilation. This term distinguishes it from mere declaration, which does not require an actual definition.

The ability to declare an entity without providing an immediate definition is crucial for modular programming, as it allows the separation of interface (header files) from implementation (source files). However, to ensure consistent behavior across compilation units, the C standard defines specific rules for when an entity must be defined.

Definition of "odr-use"

According to the C standard, an entity is "odr-used" in the following cases:

  • Its name appears as a potentially-evaluated expression, meaning it could be used in an expression that is actually evaluated
  • It is a member of a set of candidate functions during overload resolution, even if not selected
  • It is a non-pure virtual function, even if not explicitly invoked

ODR-use and Class Template Member Functions

In the context of class templates, member functions are only instantiated if they are odr-used. This means that if a class template is instantiated in one compilation unit, only the member functions that are actually called or used as part of overload resolution will be instantiated in that unit. Other member functions will remain uninstantiated until they are used.

This behavior is essential to prevent unnecessary instantiation of template members, which can significantly reduce compilation time and improve overall program efficiency. It also allows for flexibility in template design, enabling users to create generic classes and instantiate only the necessary functionality based on their specific needs.

The above is the detailed content of What Constitutes an ODR-Use in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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