{"id":1,"date":"2023-10-30T12:50:00","date_gmt":"2023-10-30T12:50:00","guid":{"rendered":"https:\/\/i.refvalue.org\/?p=1"},"modified":"2025-12-01T09:33:01","modified_gmt":"2025-12-01T01:33:01","slug":"hacking-stdsource_location","status":"publish","type":"post","link":"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/","title":{"rendered":"Inside std::source_location"},"content":{"rendered":"\n<p>Since C++20, the new revolutionary standard has introduced a meta class named <code>std::source_location<\/code> which provides information about the current line number, file name and function name of the executing context. This new class is included in a standalone header <code>&lt;source_location&gt;<\/code> and also a better alternative to the old methods like <code>__LINE__<\/code> and <code>__FILE__<\/code>.<\/p>\n\n\n\n<p><code>std::source_location<\/code> shows some magic mechanism to make an access to the code context without any macro expansions, as if it &#8216;knows&#8217; the compile-time names and numbers where it is placed, as follows: <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>#include &lt;print>\n#include &lt;source_location>\n\nnamespace {\n  void print_loc(const std::source_location&amp; loc) {\n    std::print(\"File: {}, Position: ({},{}), Function: {}\\n\",\n      loc.file_name(), loc.line(), loc.column(), loc.function_name()\n    );\n  }\n}\n\nstruct foo {\n  foo(std::source_location loc = std::source_location::current()) {\n    print_loc(loc);\n  }\n  \n  void baz(int number, std::source_location loc = std::source_location::current()) const {\n    print_loc(loc);\n  }\n};\n\nint main() {\n  const foo obj;\n  \n  obj.baz(1);\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">#include<\/span><span style=\"color: #569CD6\"> <\/span><span style=\"color: #CE9178\">&lt;print&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">#include<\/span><span style=\"color: #569CD6\"> <\/span><span style=\"color: #CE9178\">&lt;source_location&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">namespace<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #569CD6\">void<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">print_loc<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #4EC9B0\">source_location<\/span><span style=\"color: #569CD6\">&amp;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">loc<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;File: {}, Position: ({},{}), Function: {}<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">      <\/span><span style=\"color: #9CDCFE\">loc<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">file_name<\/span><span style=\"color: #D4D4D4\">(), <\/span><span style=\"color: #9CDCFE\">loc<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">line<\/span><span style=\"color: #D4D4D4\">(), <\/span><span style=\"color: #9CDCFE\">loc<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">column<\/span><span style=\"color: #D4D4D4\">(), <\/span><span style=\"color: #9CDCFE\">loc<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">function_name<\/span><span style=\"color: #D4D4D4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    );<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">struct<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">foo<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #DCDCAA\">foo<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #4EC9B0\">source_location<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">loc<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #4EC9B0\">source_location<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #4EC9B0\">current<\/span><span style=\"color: #D4D4D4\">()) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print_loc<\/span><span style=\"color: #D4D4D4\">(loc);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #569CD6\">void<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">baz<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">number<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #4EC9B0\">source_location<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">loc<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #4EC9B0\">source_location<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">current<\/span><span style=\"color: #D4D4D4\">()) <\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print_loc<\/span><span style=\"color: #D4D4D4\">(loc);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">main<\/span><span style=\"color: #D4D4D4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> foo obj;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #9CDCFE\">obj<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">baz<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>The output of the function name is implementation-defined, due to the different name mangling rules among GCC, Clang and MSVC. Both GCC and Clang respect the <a href=\"https:\/\/itanium-cxx-abi.github.io\/cxx-abi\/abi.html\">Itanium ABI<\/a>, and however Microsoft maintains its unique and uniform ABI on Windows known as the <a href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/build\/x64-software-conventions?view=msvc-170\">MSVC ABI<\/a> or Windows COM ABI.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>File: \/app\/example.cpp, Position: (23,13), Function: int main()\nFile: \/app\/example.cpp, Position: (25,10), Function: int main()<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">File: \/app\/example.cpp, Position: (23,13), Function: int main()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">File: \/app\/example.cpp, Position: (25,10), Function: int main()<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>GCC is licensed under the <a href=\"https:\/\/www.gnu.org\/licenses\/gpl-3.0.html\">GNU General Public License (GPL)<\/a>, specifically the GPLv3 with a special exception. Microsoft open-sourced its STL implementation on GitHub, with an <a href=\"https:\/\/llvm.org\/LICENSE.txt\">Apache-2.0-with-LLVM-exception<\/a> license. <a href=\"https:\/\/github.com\/gcc-mirror\/gcc\/blob\/master\/libstdc%2B%2B-v3\/include\/std\/source_location\">The latest GCC codebase<\/a> defines a new built-in function named <code>__builtin_source_location<\/code> to generate information of the call site directly. In addition, <a href=\"https:\/\/github.com\/microsoft\/STL\/blob\/main\/stl\/inc\/source_location\">MSVC&#8217;s STL<\/a> continues to use existing built-in functions such as <code>__builtin_LINE<\/code>, <code>__builtin_COLUMN<\/code>, <code>__builtin_FILE<\/code>, <code>__builtin_FUNCTION<\/code>\/<code>__builtin_FUNCSIG<\/code> that has been introduced in older versions.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly> \/\/ &#91;support.srcloc.cons&#93;, creation\nstatic consteval source_location\ncurrent(__builtin_ret_type __p = __builtin_source_location()) noexcept {\n  source_location __ret;\n  __ret._M_impl = static_cast &lt;const __impl*>(__p);\n  return __ret;\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\"> \/\/ &#91;support.srcloc.cons&#93;, creation<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">static<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">consteval<\/span><span style=\"color: #D4D4D4\"> source_location<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">current<\/span><span style=\"color: #D4D4D4\">(__builtin_ret_type __p = <\/span><span style=\"color: #DCDCAA\">__builtin_source_location<\/span><span style=\"color: #D4D4D4\">()) <\/span><span style=\"color: #569CD6\">noexcept<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  source_location __ret;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #9CDCFE\">__ret<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #9CDCFE\">_M_impl<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #569CD6\">static_cast<\/span><span style=\"color: #D4D4D4\"> &lt;<\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> __impl*&gt;(__p);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> __ret;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>_NODISCARD static consteval source_location current(const uint_least32_t _Line_ = __builtin_LINE(),\n        const uint_least32_t _Column_ = __builtin_COLUMN(), const char* const _File_ = __builtin_FILE(),\n#if _USE_DETAILED_FUNCTION_NAME_IN_SOURCE_LOCATION\n        const char* const _Function_ = __builtin_FUNCSIG()\n#else \/\/ ^^^ detailed \/ basic vvv\n        const char* const _Function_ = __builtin_FUNCTION()\n#endif \/\/ ^^^ basic ^^^\n            ) noexcept {\n  source_location _Result{};\n  _Result._Line     = _Line_;\n  _Result._Column   = _Column_;\n  _Result._File     = _File_;\n  _Result._Function = _Function_;\n  return _Result;\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">_NODISCARD <\/span><span style=\"color: #569CD6\">static<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">consteval<\/span><span style=\"color: #D4D4D4\"> source_location <\/span><span style=\"color: #DCDCAA\">current<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">uint_least32_t<\/span><span style=\"color: #D4D4D4\"> _Line_ = <\/span><span style=\"color: #DCDCAA\">__builtin_LINE<\/span><span style=\"color: #D4D4D4\">(),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">uint_least32_t<\/span><span style=\"color: #D4D4D4\"> _Column_ = <\/span><span style=\"color: #DCDCAA\">__builtin_COLUMN<\/span><span style=\"color: #D4D4D4\">(), <\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">char<\/span><span style=\"color: #D4D4D4\">* <\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> _File_ = <\/span><span style=\"color: #DCDCAA\">__builtin_FILE<\/span><span style=\"color: #D4D4D4\">(),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">#if<\/span><span style=\"color: #569CD6\"> _USE_DETAILED_FUNCTION_NAME_IN_SOURCE_LOCATION<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">char<\/span><span style=\"color: #D4D4D4\">* <\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> _Function_ = <\/span><span style=\"color: #DCDCAA\">__builtin_FUNCSIG<\/span><span style=\"color: #D4D4D4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">#else<\/span><span style=\"color: #6A9955\"> \/\/ ^^^ detailed \/ basic vvv<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">char<\/span><span style=\"color: #D4D4D4\">* <\/span><span style=\"color: #569CD6\">const<\/span><span style=\"color: #D4D4D4\"> _Function_ = <\/span><span style=\"color: #DCDCAA\">__builtin_FUNCTION<\/span><span style=\"color: #D4D4D4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">#endif<\/span><span style=\"color: #6A9955\"> \/\/ ^^^ basic ^^^<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            ) <\/span><span style=\"color: #569CD6\">noexcept<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  source_location _Result{};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #9CDCFE\">_Result<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #9CDCFE\">_Line<\/span><span style=\"color: #D4D4D4\">     = _Line_;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #9CDCFE\">_Result<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #9CDCFE\">_Column<\/span><span style=\"color: #D4D4D4\">   = _Column_;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #9CDCFE\">_Result<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #9CDCFE\">_File<\/span><span style=\"color: #D4D4D4\">     = _File_;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #9CDCFE\">_Result<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #9CDCFE\">_Function<\/span><span style=\"color: #D4D4D4\"> = _Function_;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> _Result;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>When using an early version of the compiler that does not contain <code>&lt;source_location&gt;<\/code>, writing a custom <code>source_location<\/code> class with similar functions whose values refer to the evaluation results of these built-in functions is an acceptable consideration. Literally, GCC does provide the same functions as MSVC in GCC 10 without full support of the C++20 Standard. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since C++20, the new revolutionary standard has introduced a meta class named std::source_location which provides information about the current line number, file name and function name of the executing context. This new class is included in a standalone header &lt;source_location&gt; and also a better alternative to the old methods like __LINE__ and __FILE__. std::source_location shows &hellip; <a href=\"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Inside std::source_location&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[5,7,6],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-c-standard","tag-c","tag-source_location","tag-stl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Inside std::source_location - Refvalue&#039;s Whispering Woods<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inside std::source_location - Refvalue&#039;s Whispering Woods\" \/>\n<meta property=\"og:description\" content=\"Since C++20, the new revolutionary standard has introduced a meta class named std::source_location which provides information about the current line number, file name and function name of the executing context. This new class is included in a standalone header &lt;source_location&gt; and also a better alternative to the old methods like __LINE__ and __FILE__. std::source_location shows &hellip; Continue reading &quot;Inside std::source_location&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/\" \/>\n<meta property=\"og:site_name\" content=\"Refvalue&#039;s Whispering Woods\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-30T12:50:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-12-01T01:33:01+00:00\" \/>\n<meta name=\"author\" content=\"refvalue\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"refvalue\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/index.php\\\/2023\\\/10\\\/30\\\/hacking-stdsource_location\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/index.php\\\/2023\\\/10\\\/30\\\/hacking-stdsource_location\\\/\"},\"author\":{\"name\":\"refvalue\",\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/#\\\/schema\\\/person\\\/968c7dfefa7e2b45a131a884adbbf387\"},\"headline\":\"Inside std::source_location\",\"datePublished\":\"2023-10-30T12:50:00+00:00\",\"dateModified\":\"2025-12-01T01:33:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/index.php\\\/2023\\\/10\\\/30\\\/hacking-stdsource_location\\\/\"},\"wordCount\":251,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/#\\\/schema\\\/person\\\/968c7dfefa7e2b45a131a884adbbf387\"},\"keywords\":[\"C++\",\"source_location\",\"STL\"],\"articleSection\":[\"C++ Standard\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/i.refvalue.org\\\/index.php\\\/2023\\\/10\\\/30\\\/hacking-stdsource_location\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/index.php\\\/2023\\\/10\\\/30\\\/hacking-stdsource_location\\\/\",\"url\":\"https:\\\/\\\/i.refvalue.org\\\/index.php\\\/2023\\\/10\\\/30\\\/hacking-stdsource_location\\\/\",\"name\":\"Inside std::source_location - Refvalue&#039;s Whispering Woods\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/#website\"},\"datePublished\":\"2023-10-30T12:50:00+00:00\",\"dateModified\":\"2025-12-01T01:33:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/index.php\\\/2023\\\/10\\\/30\\\/hacking-stdsource_location\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/i.refvalue.org\\\/index.php\\\/2023\\\/10\\\/30\\\/hacking-stdsource_location\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/index.php\\\/2023\\\/10\\\/30\\\/hacking-stdsource_location\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/i.refvalue.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Inside std::source_location\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/#website\",\"url\":\"https:\\\/\\\/i.refvalue.org\\\/\",\"name\":\"Refvalue's Whispering Woods\",\"description\":\"Email: cosmicbytecode@gmail.com\",\"publisher\":{\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/#\\\/schema\\\/person\\\/968c7dfefa7e2b45a131a884adbbf387\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/i.refvalue.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/i.refvalue.org\\\/#\\\/schema\\\/person\\\/968c7dfefa7e2b45a131a884adbbf387\",\"name\":\"refvalue\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b380a5ef2d3d8de84eae26847c2dcd82df59b6f96ccc5c521a74795525566720?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b380a5ef2d3d8de84eae26847c2dcd82df59b6f96ccc5c521a74795525566720?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b380a5ef2d3d8de84eae26847c2dcd82df59b6f96ccc5c521a74795525566720?s=96&d=mm&r=g\",\"caption\":\"refvalue\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b380a5ef2d3d8de84eae26847c2dcd82df59b6f96ccc5c521a74795525566720?s=96&d=mm&r=g\"},\"sameAs\":[\"https:\\\/\\\/i.refvalue.org\"],\"url\":\"https:\\\/\\\/i.refvalue.org\\\/index.php\\\/author\\\/refvalue\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Inside std::source_location - Refvalue&#039;s Whispering Woods","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/","og_locale":"en_US","og_type":"article","og_title":"Inside std::source_location - Refvalue&#039;s Whispering Woods","og_description":"Since C++20, the new revolutionary standard has introduced a meta class named std::source_location which provides information about the current line number, file name and function name of the executing context. This new class is included in a standalone header &lt;source_location&gt; and also a better alternative to the old methods like __LINE__ and __FILE__. std::source_location shows &hellip; Continue reading \"Inside std::source_location\"","og_url":"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/","og_site_name":"Refvalue&#039;s Whispering Woods","article_published_time":"2023-10-30T12:50:00+00:00","article_modified_time":"2025-12-01T01:33:01+00:00","author":"refvalue","twitter_card":"summary_large_image","twitter_misc":{"Written by":"refvalue","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/#article","isPartOf":{"@id":"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/"},"author":{"name":"refvalue","@id":"https:\/\/i.refvalue.org\/#\/schema\/person\/968c7dfefa7e2b45a131a884adbbf387"},"headline":"Inside std::source_location","datePublished":"2023-10-30T12:50:00+00:00","dateModified":"2025-12-01T01:33:01+00:00","mainEntityOfPage":{"@id":"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/"},"wordCount":251,"commentCount":0,"publisher":{"@id":"https:\/\/i.refvalue.org\/#\/schema\/person\/968c7dfefa7e2b45a131a884adbbf387"},"keywords":["C++","source_location","STL"],"articleSection":["C++ Standard"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/","url":"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/","name":"Inside std::source_location - Refvalue&#039;s Whispering Woods","isPartOf":{"@id":"https:\/\/i.refvalue.org\/#website"},"datePublished":"2023-10-30T12:50:00+00:00","dateModified":"2025-12-01T01:33:01+00:00","breadcrumb":{"@id":"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/i.refvalue.org\/index.php\/2023\/10\/30\/hacking-stdsource_location\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/i.refvalue.org\/"},{"@type":"ListItem","position":2,"name":"Inside std::source_location"}]},{"@type":"WebSite","@id":"https:\/\/i.refvalue.org\/#website","url":"https:\/\/i.refvalue.org\/","name":"Refvalue's Whispering Woods","description":"Email: cosmicbytecode@gmail.com","publisher":{"@id":"https:\/\/i.refvalue.org\/#\/schema\/person\/968c7dfefa7e2b45a131a884adbbf387"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/i.refvalue.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/i.refvalue.org\/#\/schema\/person\/968c7dfefa7e2b45a131a884adbbf387","name":"refvalue","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b380a5ef2d3d8de84eae26847c2dcd82df59b6f96ccc5c521a74795525566720?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b380a5ef2d3d8de84eae26847c2dcd82df59b6f96ccc5c521a74795525566720?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b380a5ef2d3d8de84eae26847c2dcd82df59b6f96ccc5c521a74795525566720?s=96&d=mm&r=g","caption":"refvalue"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/b380a5ef2d3d8de84eae26847c2dcd82df59b6f96ccc5c521a74795525566720?s=96&d=mm&r=g"},"sameAs":["https:\/\/i.refvalue.org"],"url":"https:\/\/i.refvalue.org\/index.php\/author\/refvalue\/"}]}},"_links":{"self":[{"href":"https:\/\/i.refvalue.org\/index.php\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i.refvalue.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i.refvalue.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i.refvalue.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i.refvalue.org\/index.php\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":22,"href":"https:\/\/i.refvalue.org\/index.php\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":2643,"href":"https:\/\/i.refvalue.org\/index.php\/wp-json\/wp\/v2\/posts\/1\/revisions\/2643"}],"wp:attachment":[{"href":"https:\/\/i.refvalue.org\/index.php\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i.refvalue.org\/index.php\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i.refvalue.org\/index.php\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}