inline functions and the one definition rule

Viewed 520

inline functions provide something of a weakening of the one definition rule -- multiple definitions are allowed, albeit with some restrictions. One wording I found online is

The requirements are that each definition should be the same, meaning it should consist of the same tokens and refer to the same items.

though I admit that I don't know if that's definitive. I'm also not sure how strict it is.

I'm considering cases in which I create headers with class definitions and/or inline functions that I want to be #include-able whether one is compiling in C++03, C++11, or even some later standard. It would be natural to use the __cplusplus macro to conditionally change the code, but will the ODR come into play? For example, it seems reasonable to conditionally:

  • provide a nested typedef.
  • provide a nested class.
  • provide move-related functions.
  • mark a function throw() or noexcept. (This is particularly important since every destructor picks up an implicit noexcept in C++11.)
  • mark a function constexpr.
  • mark a function with override and/or final.
  • mark a function [[noreturn]] and/or [[nodiscard]].
  • mark a parameter [[maybe_unused]].
  • use [[fallthrough]] within a function body.

but which of these -- if any -- are actually allowed if one wants to enable libraries that #include such headers to be compiled under different standards and still be safely used together?

1 Answers
Related