std::source_location::line
From cppreference.com
< cpp | utility | source location
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
std::source_location
Member functions
Creation
Field access
source_location::line
constexpr std::uint_least32_t line() const noexcept;
(since C++20)
Returns the line number represented by this object.
Contents
[edit] Parameters
(none)
[edit] Return value
The line number represented by this object.
An implementation is encouraged to return 0
when the line number is unknown.
[edit] Example
Run this code
#include <iostream> #include <string_view> #include <source_location> inline void cur_line( const std::string_view message = "", const std::source_location & location = std::source_location::current()) { std::cout << location.line() // <- the line # of a call site << ") " << message; } int main() { cur_line("++\n"); cur_line(); std::cout << "Hello,\n"; cur_line(); std::cout << "C++20!\n"; cur_line("--\n"); }
Output:
17) ++ 18) Hello, 19) C++20! 20) --
[edit] See also
gets the line number that lexically relates the evaluation represented by the
(public member function of
stacktrace_entry
(public member function of
std::stacktrace_entry
) [edit]