std::experi..._source_line(3)

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

πŸ’… std::experimental::reflect::get_source_line

okay bestie so this one lives in the C++ Standard Libary (yes that's the cppreference spelling, we don't gatekeep typos around here 🫢) and it's a reflection TS situation. Buckle up, it's actually so cute and simple.


✨ NAME ✨

std::experimental::reflect::get_source_line β€” and yeah, it's literally that, twice, because the name is the whole personality. It tells you what line a thing was declared on. That's it. That's the vibe. πŸ’–


🌸 SYNOPSIS 🌸

Defined in header <experimental/reflect>:

template< Object T >                      (reflection TS)
struct get_source_line;

So it's a struct template over Object T, and it gives you a member constant value that's equal to the presumed line number of the declaration of the entity or typedef-name reflected by T. Obsessed. πŸ”₯


πŸŽ€ MEMBER CONSTANTS πŸŽ€

  • value β€” the presumed line number of the declaration of the entity or typedef-name reflected by T. It's [static], and it's a public static member constant. Iconic behavior, no notes. ✨

πŸ¦‹ MEMBER FUNCTIONS πŸ¦‹

  • operator std::uint_least32_t β€” converts the object to std::uint_least32_t, and returns value. Public member function, doing its little conversion thing. πŸ’œ
  • operator() (C++14) β€” returns value. Public member function. That's it, she's lowkey just handing you the number. 🫢

🌈 MEMBER TYPES 🌈

  • value_type β€” defined as std::uint_least32_t
  • type β€” defined as std::integral_constant<std::uint_least32_t, value>

(the original lays these out in a little table with columns Type / Definition β€” same facts, cuter packaging 🎁)


πŸ“ EXAMPLE πŸ“

The following code shows the source code line of an object or a class. Run this code, bestie:

#include<experimental/reflect>
#include<iostream>

using refl = std::experimental::reflect;

float f;
struct P{};

int main()
{
    std::cout << refl::get_source_line_v<reflexpr(f)> << '\n';
    std::cout << refl::get_source_line_v<reflexpr(P)> << '\n';
}

πŸ’€ Output

6
7

See, f is on line 6 and P is on line 7 β€” counting from the top of the file, #include lines included. The way it just knows?? Not me being emotionally attached to a compile-time constant. 😭


⭐ SEE ALSO ⭐

  • line β€” return the line number represented by this object (public member function of std::source_location)
  • 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)

like, they're cousins. same energy, different standards. iykyk πŸ‘‘


ngl, this is a tiny little reflection utility β€” no warnings, no deprecated anything, no bugs to report. just a struct that knows where stuff lives. main character energy fr fr 🧚

source: http://cppreference.com Β· version 2024.06.10 Β· the manual page formally known as std::experi..._source_line(3) β€” mwah, that's the sign-off, hope this slayed for you bestie πŸ’…πŸ’–βœ¨

πŸ“¦ where this page comes from