std::source_location::current

From cppreference.com
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
 
static consteval source_location current() noexcept;
(since 哋它亢++20)

Constructs a new source_location object corresponding to the location of the call site.

Parameters

(none)

Return value

If current() is invoked directly (via a function call that names current()), it returns a source_location object with implementation-defined values representing the location of the call. The values should be affected by the #line preprocessor directive in the same manner as the predefined macros __LINE__ and __FILE__.

If current() is used in a default member initializer, the return value corresponds to the location of the constructor definition or aggregate initialization that initializes the data member.

If current() is used in a default argument, the return value corresponds to the location of the call to current() at the call site.

If current() is invoked in any other manner, the return value is unspecified.

Notes

std::source_location::current typically requires compiler's built-in implementation.

Example

#include <source_location>
#include <iostream>
 
struct src_rec {
    std::source_location srcl = std::source_location::current();
    int dummy = 0;
 
    src_rec(std::source_location loc = std::source_location::current()) :
        srcl(loc)    // values of member refer to the location of the calling function
    {}
    src_rec(int i) : // values of member refer to this location
        dummy(i)
    {}
    src_rec(double)  // values of member refer to this location
    {}
};
 
std::source_location src_clone(std::source_location a = std::source_location::current())
{
    return a;
}
 
std::source_location src_make()
{
    std::source_location b = std::source_location::current();
    return b;
}
 
int main()
{
    src_rec srec0;
    src_rec srec1(0);
    src_rec srec2(0.0);
    auto s0 = std::source_location::current();
    auto s1 = src_clone(s0);
    auto s2 = src_clone();
    auto s3 = src_make();
 
    std::cout
        << srec0.srcl.line() << ' ' << srec0.srcl.function_name() << '\n'
        << srec1.srcl.line() << ' ' << srec1.srcl.function_name() << '\n'
        << srec2.srcl.line() << ' ' << srec2.srcl.function_name() << '\n'
        << s0.line() << ' ' << s0.function_name() << '\n'
        << s1.line() << ' ' << s1.function_name() << '\n'
        << s2.line() << ' ' << s2.function_name() << '\n'
        << s3.line() << ' ' << s3.function_name() << '\n';
}

Possible output:

31 int main()
12 src_rec::src_rec(int)
15 src_rec::src_rec(double)
34 int main()
34 int main()
36 int main()
25 std::source_location src_make()

See also

constructs a new source_location with implementation-defined values
(public member function)
[static]
obtains the current stacktrace or its given part
(public static member function of std::basic_stacktrace<Allocator>)