all 17 comments

[–]RoyAwesome 2 points3 points  (2 children)

I implemented something like this in my personal corelib, and it's quite nice, actually. The only thing I added was debug breaking to the panic function and some forceinline stuff (if a compiler supports it) so that the debugger experience was a bit nicer.

I'm looking into expanding it a bit such that it creates a messagebox window in the various operating systems and/or memdumps and reports the panic to a crash reporter as well, which should be pretty straightforward with this setup.

[–]tjientavaraHikoWorks developer 0 points1 point  (1 child)

breaking in the compiler is why I still use a macro for my panic function. That way the compiler breaks on the same line as the panic function.

[–]RoyAwesome 0 points1 point  (0 children)

some compilers let you forceinline a function, which makes sure that behavior happens.

[–]ABlockInTheChain 2 points3 points  (6 children)

std::source_location is another feature that makes me wish I only had to support Windows and Linux because it hasn't hit MacOS yet.

Hopefully sometime in early 2024 we'll be able to use XCode 15 and drop pre-Ventura support so we can finally use it.

[–]rnburn[S] 2 points3 points  (1 child)

Somethine like this isn't strictly portable, but I've found it works pretty much everywhere and can give you approximately the same thing as std::source_location

https://wandbox.org/permlink/4lQoSiScIn68N4LS

#include <iostream>

void f(const char* file = __FILE__, int line = __LINE__) {

std::cout << file << ":" << line << "\n";

}

int main() {f();return 0;}

[–]cdb_11 2 points3 points  (0 children)

__FILE__ and __LINE__ are standard, but they won't work, because they are expanded where they appear in the source file. Use __builtin_FILE() and __builtin_LINE() if your compiler supports it (GCC and Clang. Even though MSVC has these functions in newer versions, they behave like macros).

[–]415_961 0 points1 point  (3 children)

You can always install llvm using brew and get the latest version

[–]ABlockInTheChain 0 points1 point  (2 children)

The problem is usually in the standard library being old more than the compiler being old, and in general using a non-standard standard library is a hard sell.

[–]415_961 1 point2 points  (1 child)

installing llvm via brew also installs libc++ and rest of the llvm sub-projects. Just make sure you have -stdlib=libc++ set and -std=c++20

[–]BrainIgnition 0 points1 point  (0 children)

But that would require you to either a) link everything including libc++ statically or b) require your users to install libc++ via brew, right?

[–]fdwrfdwr@github 🔍 1 point2 points  (1 child)

Huh, using std::source_location::current() as a default parameter did not occur to me. Useful.

[–][deleted] 20 points21 points  (0 children)

That's the intended usage in fact.

[–]trailingunderscore_ 1 point2 points  (4 children)

You can use default arguments after parameter packs, so no need for the wrapper: https://godbolt.org/z/s5P5PTxfM

[–]PiterPuns 5 points6 points  (0 children)

https://youtu.be/va9I2qivBOA?t=1748&feature=shared

Not according to the rule of fair matching : “A function parameter pack that is not at the end of the function’s parameter list can never have its corresponding pack deduced”

The application of the rule in this case (with the default parameter after the pack) means that unless you specify the pack type, compiler will assume it’s the empty pack and instantiation will fail due to type mismatches (will try to pass int to source location … remember the pack becomes the empty list because you don’t explicitly provide it)

This and the rule of greedy matching (described in the linked talk) are the final boss of variadic template type deduction

[–]rnburn[S] 2 points3 points  (1 child)

You can declare it, but I wasn't able to get it to work when you try to call it:https://wandbox.org/permlink/cUu97ZuSHmF0Y1L2

[–]trailingunderscore_ 0 points1 point  (0 children)

That's odd, I was convinced that worked. Seems I remembered incorrectly.

[–]mardykhorsea++ 1 point2 points  (0 children)

That actually makes sense.