std::source_location::current is unimplementable
6.3 [basic.def.odr]
Richard Smith

Created on 2023-01-07.00:00:00 last changed 3 weeks ago


Date: 2023-01-07.00:00:00


  #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.


Forwarded to LWG / LEWG, by decision of the CWG chair.

Date User Action Args
2023-01-07 00:00:00admincreate