std::experi...ce_location(3)

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

πŸ’… NAME πŸ’…

std::experimental::source_location β€” bestie, it's literally std::experimental::source_location, that's the whole name, no notes ✨ (and yes, this page lives in the C++ Standard Libary man page, we're all friends here 🫢)


✨ SYNOPSIS ✨

So first things first, the vibe check on where this lil guy lives:

Defined in header <experimental/source_location>
struct source_location;                           (library fundamentals TS v2)

The source_location class represents certain information about the source code, such as file names, line numbers, and function names. Like, it's giving "who am I, where do I come from" but for code πŸ”₯

Now for the backstory, because it's iconic πŸ‘‘: previously, functions that desire to obtain this information about the call site (for logging, testing, or debugging purposes) must use macros so that predefined macros like __LINE__ and __FILE__ are expanded in the context of the caller. The source_location class provides a better alternative. Periodt. No more macro spaghetti, we're classy now πŸ’…


πŸŽ€ MEMBER FUNCTIONS πŸŽ€

🌸 Creation

  • constructor β€” constructs a new source_location with implementation-defined values (public member function). Ngl, the values are implementation-defined, so don't come for me if yours look different πŸ™ˆ
  • current [static] β€” constructs a new source_location (public static member function). This is the main character energy one right here ⭐

πŸ¦‹ Other special member functions

  • destructor (implicitly declared) β€” destructs a source_location (public member function). It's giving "my time has come" πŸ’€
  • operator= (implicitly declared) β€” implicitly declared copy/move assignment operators (public member function). Free of charge, we love a no-effort slay ✨

πŸ’œ Field access

  • line β€” return the line number represented by this object (public member function)
  • 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). Note the "if any" β€” sometimes it just doesn't know, and that's okay 😭

🌈 EXAMPLE 🌈

Okay so here's the code, run it, it ate fr fr πŸ”₯

// Run this code

#include <experimental/source_location>
#include <iostream>
#include <string_view>

void log(const std::string_view message,
         const std::experimental::source_location location =
               std::experimental::source_location::current())
{
    std::cout << "info:"
              << location.file_name() << ':'
              << location.line() << ' '
              << message << '\n';
}

int main()
{
    log("Hello world!");
}

Look at that default argument doing the absolute most β€” it's grabbing the call site like a little detective πŸ•΅οΈβ€β™€οΈ obsessed.


πŸ’– POSSIBLE OUTPUT πŸ’–

Possible output:
info:main.cpp:15 Hello world!

The way it just knows it was main.cpp, line 15... iykyk 🧚


Okay that's the whole page bestie, hope this was helpful, mwah πŸ“

Sign-off szn: this iconic little page came from http://cppreference.com, version 2024.06.10, and it's the std::experi...ce_location(3) page β€” slay, bye πŸ‘‹πŸ’–

πŸ“¦ where this page comes from