std::source_location::line(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::source_location::line(3) πŸ’–

okay bestie, welcome to the C++ Standard Libary page for std::source_location::line β€” manual name folded in near the top like a lil’ name tag, because we’re cute AND accurate 🫢


🌸 NAME 🌸

std::source_location::line β€” std::source_location::line (iconic, no notes πŸ’…)


✨ Synopsis ✨

Here’s the signature, no cap:

constexpr std::uint_least32_t line() const noexcept;  (since C++20)

Returns the line number represented by this object. That’s the whole vibe, fr fr.


πŸŽ€ Parameters πŸŽ€

(none) β€” literally zero parameters, she takes no arguments and still serves πŸ’…


πŸ¦‹ Return value πŸ¦‹

The line number represented by this object. ✨

An implementation is encouraged to return 0 when the line number is unknown. So if you see 0, that’s the β€œidk” answer β€” highkey useful to know, iykyk.


πŸ’» Example πŸ’»

Run this code πŸ‘‡

// 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) --

Ate, periodt. πŸ’–


πŸ‘€ See also πŸ‘€

  • column β€” return the column number represented by this object (public member function)
  • file_name β€” return the file name represented by this object (public member function)
  • function_name β€” return the name of the function represented by this object, if any (public member function)
  • source_line (C++23) β€” gets the line number that lexically relates the evaluation represented by the stacktrace_entry (public member function of std::stacktrace_entry)
  • Filename and line information

kisses and compiler blessings πŸ’œ β€” from http://cppreference.com, dated 2024.06.10, for std::source_location::line(3) 🧚

πŸ“¦ where this page comes from