Files
cppdraft_translate/cppdraft/support/srcloc.md
2025-10-25 03:02:53 +03:00

9.8 KiB

[support.srcloc]

17 Language support library [support]

17.8 Source location [support.srcloc]

17.8.1 Header <source_location> synopsis [source.location.syn]

1

#

The header <source_location> defines the class source_location that provides a means to obtain source location information.

// all freestandingnamespace std {struct source_location;}

17.8.2 Class source_location [support.srcloc.class]

17.8.2.1 General [support.srcloc.class.general]

🔗

namespace std {struct source_location {// source location constructionstatic consteval source_location current() noexcept; constexpr source_location() noexcept; // source location field accessconstexpr uint_least32_t line() const noexcept; constexpr uint_least32_t column() const noexcept; constexpr const char* file_name() const noexcept; constexpr const char* function_name() const noexcept; private: uint_least32_t line_; // exposition only uint_least32_t column_; // exposition onlyconst char* file_name_; // exposition onlyconst char* function_name_; // exposition only};}

1

#

The type source_location meets theCpp17DefaultConstructible,Cpp17CopyConstructible,Cpp17CopyAssignable,Cpp17Swappable, andCpp17Destructible requirements ([utility.arg.requirements], [swappable.requirements]).

All of the following conditions are true:

is_nothrow_move_constructible_v<source_location>

is_nothrow_move_assignable_v<source_location>

is_nothrow_swappable_v<source_location>

[Note 1:

The intent of source_location is to have a small size and efficient copying.

It is unspecified whether the copy/move constructors and the copy/move assignment operators are trivial and/or constexpr.

— end note]

2

#

The data members file_name_ and function_name_ always each refer to an ntbs.

3

#

The copy/move constructors and the copy/move assignment operators ofsource_location meet the following postconditions: Given two objects lhs and rhs of type source_location, where lhs is a copy/move result of rhs, and where rhs_p is a value denoting the state of rhs before the corresponding copy/move operation, then each of the following conditions is true:

strcmp(lhs.file_name(), rhs_p.file_name()) == 0

strcmp(lhs.function_name(), rhs_p.function_name()) == 0

lhs.line() == rhs_p.line()

lhs.column() == rhs_p.column()

17.8.2.2 Creation [support.srcloc.cons]

🔗

static consteval source_location current() noexcept;

1

#

Returns:

  • (1.1)

    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 43. Table 43 — Value of object returned by current [tab:support.srcloc.current]

🔗
Element
Value
🔗
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.
  • (1.2)

    Otherwise, when invoked in some other way, returns a source_location whose data members are initialized with valid but unspecified values.

2

#

Remarks: Any call to current that appears as a default member initializer ([class.mem.general]), or as a subexpression thereof, should correspond to the location of the constructor definition or aggregate initialization that uses the default member initializer.

Any call to current that appears as a default argument ([dcl.fct.default]), or as a subexpression thereof, should correspond to the location of the invocation of the function that uses the default argument ([expr.call]).

3

#

[Example 1: 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;

4

#

Effects: The data members are initialized with valid but unspecified values.

17.8.2.3 Observers [support.srcloc.obs]

🔗

constexpr uint_least32_t line() const noexcept;

1

#

Returns: line_.

🔗

constexpr uint_least32_t column() const noexcept;

2

#

Returns: column_.

🔗

constexpr const char* file_name() const noexcept;

3

#

Returns: file_name_.

🔗

constexpr const char* function_name() const noexcept;

4

#

Returns: function_name_.