17 Language support library [language.support]

17.8 Source location [support.srcloc]

17.8.2 Class source_­location [support.srcloc.class]

17.8.2.1 Creation [support.srcloc.cons]

static consteval source_location current() noexcept;
Returns:
  • When invoked by a function call whose postfix-expression is a (possibly parenthesized) id-expression naming current, returns a source_­location with an implementation-defined value. The value should be affected by #line ([cpp.line]) in the same manner as for __LINE__ and __FILE__. The values of the exposition-only data members of the returned source_­location object are indicated in Table 36.
    Table 36: Value of object returned by current   [tab:support.srcloc.current]
    ElementValue
    line_­A presumed line number ([cpp.predefined]). Line numbers are presumed to be 1-indexed; however, an implementation is encouraged to use 0 when the line number is unknown.
    column_­An implementation-defined value denoting some offset from the start of the line denoted by line_­. Column numbers are presumed to be 1-indexed; however, an implementation is encouraged to use 0 when the column number is unknown.
    file_­name_­A presumed name of the current source file ([cpp.predefined]) as an ntbs.
    function_­name_­A name of the current function such as in __func__ ([dcl.fct.def.general]) if any, an empty string otherwise.
  • Otherwise, when invoked in some other way, returns a source_­location whose data members are initialized with valid but unspecified values.
Remarks: When a default member initializer is used to initialize a non-static data member, any calls to current should correspond to the location of the constructor or aggregate initialization that initializes the member.
Note
:
When used as a default argument ([dcl.fct.default]), the value of the source_­location will be the location of the call to current at the call site.
— end note
 ]
Example
:
struct s {
  source_location member = source_location::current();
  int other_member;
  s(source_location loc = source_location::current())
    : member(loc)               // values of member refer to the location of the calling function ([dcl.fct.default])
  {}
  s(int blather) :              // values of member refer to this location
    other_member(blather)
  {}
  s(double)                     // values of member refer to this location
  {}
};
void f(source_location a = source_location::current()) {
  source_location b = source_location::current();       // values in b refer to this line
}

void g() {
  f();                          // f's first argument corresponds to this line of code

  source_location c = source_location::current();
  f(c);                         // f's first argument gets the same values as c, above
}
— end example
 ]
constexpr source_location() noexcept;
Effects: The data members are initialized with valid but unspecified values.