Title
std::source_location::current is unimplementable
Status
c++23
Section
6.3 [basic.def.odr]
Submitter
Richard Smith

Created on 2023-01-07.00:00:00 last changed 16 months ago

Messages

Date: 2023-02-09.03:05:16

Proposed resolution (approved by CWG 2023-02-08):

Add a new bullet after 6.3 [basic.def.odr] bullet 14.8 as follows:

  • In each such definition, const objects with static or thread storage duration shall be constant-initialized if the object is constant-initialized in any such definition.
  • In each such definition, corresponding manifestly constant-evaluated expressions that are not value-dependent shall have the same value (7.7 [expr.const], 13.8.3.4 [temp.dep.constexpr]).
  • In each such definition, the overloaded operators referred to, the implicit calls to conversion functions, constructors, operator new functions and operator delete functions, shall refer to the same function.
Date: 2023-02-15.00:00:00

[Accepted as a DR at the February, 2023 meeting.]

Consider:

  #include <source_location>

  inline char *f() {
    static char array[std::source_location::current().line()];
    return array;
  }

The sequence of tokens comprising the definition of f can appear in multiple translation units, on different lines. The one-definition rule is not violated. Thus, there is a single function f in the program with a unique static local object array, but that object would have a different type in each translation unit. It is unclear how to implement this, absent the conservative approach of always returning a value-initialized object from std::source_location::current, which would defeat its purpose.

Possible approaches to resolve this issue might include:

  • Do not mark the source_location accessors constexpr; they are not deterministic.
  • Prohibit calls to source_location accessors during constant evaluation unless the current() call happened in a context that can only appear once in a program.
  • Do not mark the current function constexpr / consteval.
  • Change the ODR to prohibit calling the current function in an inline function or in a function template.

2023-01-08

Forwarded to LWG / LEWG via cplusplus/papers#1416, by decision of the CWG chair.

EWG 2023-02-07

EWG approves of the approach for CWG2678 of changing the ODR to make use of source_location in a way that causes an inline function/function template/etc to 'be different' be an ODR violation.

History
Date User Action Args
2023-07-16 13:00:43adminsetstatus: open -> c++23
2023-07-16 13:00:43adminsetstatus: dr -> open
2023-02-18 18:43:04adminsetstatus: ready -> dr
2023-02-10 23:01:52adminsetstatus: tentatively ready -> ready
2023-02-09 03:05:16adminsetmessages: + msg7179
2023-02-09 03:05:16adminsetstatus: open -> tentatively ready
2023-01-07 00:00:00admincreate