Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Filter by
Sorted by
Tagged with
2 votes
1 answer
149 views

Compile-time manipulation of std::source_location::current()

I want to write a function that can obtain caller's function name, or at least its length, at compile time. I think obtaining std::source_location::current() is close to what I want and currently I'm ...
GKxx's user avatar
  • 578
0 votes
0 answers
18 views

Printing std::source_location attributes in __cyg_profile_func_enter causes segfault?

I'm playing around with -finstrument-functions, and I'm running into things I don't understand Let's take the following dead-simple program // main.cc #include <iostream> #include <...
bleh's user avatar
  • 1
5 votes
2 answers
188 views

Is std::source_location guaranteed to reuse the same object?

My question is: If std::source_location::current() is called multiple times for the same location, is the std::source_location object guaranteed to be the same for every call? In other words, given ...
Walter Svenddal's user avatar
-1 votes
1 answer
208 views

How to get data member names with source_location::function_name

After reading this source (and also my answer) and this source, I got the impression that we can use std::source_location::function_name to extract names of data members. Let's say we are given some ...
PavelDev's user avatar
  • 713
-1 votes
2 answers
284 views

c++ string formatting function requiring constant expression to be used inside a logging function

I'm trying to create a logging function, that will write formatted string to a buffer. The function should automatically add source code location to the formatted string using std::source_location. ...
Radim Kalaš's user avatar
0 votes
0 answers
85 views

C++ correct usage of std::source_location [duplicate]

I'm trying to use std::source_location in combination with a logging mechanism. However it turns out that std::source_location points to the wrong location. For reference the compiler explorer link ...
JuliusCaesar's user avatar
  • 1,041
3 votes
3 answers
939 views

constructing std::source_location with custom values

I am integrating a third-party library into my project. The library provides hooks to redirect its log messages and provides a struct with file, line, severity, message, etc. My project uses std::...
Tomas's user avatar
  • 123
1 vote
0 answers
266 views

C++20 source_location template non-type parameter

I was trying to figure out ways to use std::source_location::current(), when I stumbled upon this particular answer for a thread. I tried running the code on godbolt with x86-64 gcc 13.1 and -O3 -std=...
spaL's user avatar
  • 718
0 votes
2 answers
3k views

How can I pass CMake's CMAKE_SOURCE_DIR to C++ source files?

In my C++ program I use std::source_location for log output. The printed path is currently absolute. The project is built with CMake. I want to shorten the std::source_location path according to the ...
Benjamin Buch's user avatar
4 votes
1 answer
150 views

Source location at call site and nttps: strange results and possible compiler bug?

Consider the following code that uses source_location: // Preamble #include <iostream> #include <source_location> // Using a function constexpr std::size_t function( std::size_t line =...
Vincent's user avatar
  • 61.1k
0 votes
1 answer
277 views

Replacing __LINE__ and __FUNCSIG__ with the new std::source_location in a macro

C++20 added std::source_location as a replacement for the debugging macros __LINE__, __FILE__, etc. This is great. I have a macro that builds up a variable declaration in order to log and profile a ...
Casey's user avatar
  • 11k
0 votes
1 answer
587 views

operator[] caller's site source location current workaround

sadly, the current source location can't be used directly in the parameter list of operator[], as this operator has to have only one argument. However, is there a workaround so I can get the callers ...
user3520616's user avatar
0 votes
1 answer
471 views

How to disable copy elision for constructors with std::source_location?

I am trying to add instrumentation to a widely used template class in my product. I am currently on VS 2019 (16.10.4) with /std:c++17. The new feature of std::source_location is a great addition for ...
Rahul Bhobe's user avatar
  • 4,471
2 votes
1 answer
407 views

Combining ranges adaptors and std::source_location got strange results

Consider the following useless code: #include <ranges> #include <source_location> #include <iostream> int main() { auto lines = std::views::iota(0, 5) | std::views::...
康桓瑋's user avatar
  • 46.7k
1 vote
0 answers
50 views

source_location is not a member of std [duplicate]

I'm getting this error "source_location is not a member of std" in this simple program and I have no idea why. I'm trying to work with source location but cannot get the header included ...
Thomas Marsden's user avatar
2 votes
1 answer
786 views

How to get a source location from the function as argument without using static member function `current`?

I want to my function get_source to return a std::source_location type by taking a another function as an argument. For example, I have a function named helloMessage and sumOf with the following ...
Desmond Gold's user avatar
  • 2,278
6 votes
1 answer
347 views

C++20 std::source_location yield different column numbers between free function and template functions

Consider the template function g() and free function f(): #include <iostream> #include <source_location> auto g(auto...) { std::cout << std::source_location::current().column() <&...
康桓瑋's user avatar
  • 46.7k
4 votes
1 answer
1k views

std::source_location as non type template parameter

In my infinite quest to push limits of what can be used as non type template parameter I was trying to see if I can use std::source_location as non type template parameter. That failed with a weird ...
NoSenseEtAl's user avatar
  • 30.9k
1 vote
0 answers
553 views

source_location::current() evaluated as default non-type template argument

[support.srcloc] introduces source_location::current: Any call to current that appears as a default argument ([dcl.fct.default]), or as a subexpression thereof, should correspond to the location of ...
VainMan's user avatar
  • 2,399
3 votes
2 answers
672 views

consteval wrapper vs. source_location

I tried the following first variant using GCC10 in C++20 mode: consteval std::experimental::source_location there() { return std::experimental::source_location::current(); // Line 3 } void f(const ...
Silicomancer's user avatar
  • 9,384
0 votes
0 answers
35 views

Why are std::source_location's getters not marked as [[nodiscard]]? [duplicate]

According to: https://en.cppreference.com/w/cpp/utility/source_location the getters aren't marked as [[nodiscard]]. constexpr uint_least32_t line() const noexcept; constexpr uint_least32_t ...
Alexander Karatarakis's user avatar
110 votes
2 answers
12k views

Does C++20 mandate source code being stored in files?

A slightly strange question, however, if I remember correctly, C++ source code doesn't require a file system to store its files. Having a compiler that scans handwritten papers via a camera would be ...
JVApen's user avatar
  • 11.5k
80 votes
8 answers
10k views

How to use source_location in a variadic template function?

The C++20 feature std::source_location is used to capture information about the context in which a function is called. When I try to use it with a variadic template function, I encountered a problem: ...
L. F.'s user avatar
  • 20.9k
1 vote
3 answers
333 views

Is it possible to make macros that are default arguments expand at call site?

#include <stdio.h> void print(int a = __LINE__){printf("hello %d\n", a);} void main(){ print(); print(); print(); print(); } The __LINE__ macro in this case expands to 3, so the print ...
pulp_user's user avatar
  • 2,866
8 votes
1 answer
2k views

std::experimental::source_location at compile time

std::experimental::source_location will probably be added to the C++ standard at some point. I'm wondering if it possible to get the location information into the compile-time realm. Essentially, I ...
levzettelin's user avatar
  • 3,062
2 votes
1 answer
2k views

std::experimental::source_location implementation in visual studio

A reasonably conforming version of std::experimental::source_location can be implemented in gcc with __builtin_FILE(), __builtin_LINE(), etc. Do similar intrinsics exist in Visual Studio 2017? Or is ...
Mike's user avatar
  • 702
16 votes
1 answer
7k views

How could std::experimental::source_location be implemented?

C++ Extensions for Library Fundamentals, Version 2 (N4564) introduces the type std::experimental::source_location. § 14.1.2 [reflection.src_loc.creation] says: static constexpr source_location ...
5gon12eder's user avatar
  • 25.5k