Namespaces
Variants
Views
Actions

std::source_location::line

From cppreference.com
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
 
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

#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 call-site line #
        << ") "
        << message; 
}
 
auto main() -> int
{
    cur_line("++" "\n");
    cur_line(); std::cout << "Hello," "\n";
    cur_line(); std::cout << "C++20!" "\n";
    cur_line("--" "\n");
}

Output:

18) ++
19) Hello, 
20) C++20! 
21) --

[edit] See also

return the column number represented by this object
(public member function) [edit]
return the file name represented by this object
(public member function) [edit]
return the name of the function represented by this object, if any
(public member function) [edit]
C++ documentation for Filename and line information