Questions tagged [std-source-location]

A C++ standard library construct for accessing the location of a line of source code, at compile time.

C++20 introduced a class which enables compile-time access and manipulation of the location of lines of source code. With earlier standard versions of the language, one had to avail one's self of the __FILE__ and __LINE__ macros for a similar functionality.

See a longer description with examples on cppreference.com.

22 questions
109
votes
2 answers

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 a conforming implementation. Although practically…
JVApen
  • 11,008
  • 5
  • 31
  • 67
66
votes
7 answers

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: I can't see a place to put the source_location…
L. F.
  • 19,445
  • 8
  • 48
  • 82
16
votes
1 answer

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 current() noexcept; Returns: When invoked by a…
5gon12eder
  • 24,280
  • 5
  • 45
  • 92
8
votes
1 answer

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 want a function that returns different types when…
6
votes
1 answer

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 #include auto g(auto...) { std::cout << std::source_location::current().column() << "\n"; } auto f() { std::cout <<…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
4
votes
1 answer

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

Consider the following code that uses source_location: // Preamble #include #include // Using a function constexpr std::size_t function( std::size_t line = std::source_location::current().line() ) noexcept {return…
Vincent
  • 57,703
  • 61
  • 205
  • 388
3
votes
2 answers

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 std::experimental::source_location& a = there())…
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
2
votes
3 answers

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::source_location for logging. How do I construct…
Tomas
  • 105
  • 7
2
votes
1 answer

Combining ranges adaptors and std::source_location got strange results

Consider the following useless code: #include #include #include int main() { auto lines = std::views::iota(0, 5) | std::views::transform( [](int, const std::source_location&…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
2
votes
1 answer

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 argument types and return type: void…
Desmond Gold
  • 1,517
  • 1
  • 7
  • 19
2
votes
1 answer

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 message, since I presume source_location is some…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
2
votes
1 answer

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 there any way to implement…
1
vote
0 answers

source_location is not a member of std

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 properly. I'm using Visual studio 2019 version 16.10.3 and…
1
vote
0 answers

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 the invocation of the function that uses the…
VainMan
  • 2,025
  • 8
  • 23
1
vote
3 answers

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

#include 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 function is called 4 times with the same value. Is…
pulp_user
  • 2,454
  • 3
  • 14
  • 18
1
2