std::experi...ce_location(3)
π 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 newsource_locationwith 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 newsource_location(public static member function). This is the main character energy one right here β
π¦ Other special member functions
destructor(implicitly declared) β destructs asource_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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms