Skip to content

Instantly share code, notes, and snippets.

@jdiego
Created October 17, 2019 11:35
Show Gist options
  • Save jdiego/c2f70966aa66729eda07d215de7cbb5e to your computer and use it in GitHub Desktop.
Save jdiego/c2f70966aa66729eda07d215de7cbb5e to your computer and use it in GitHub Desktop.
How to use source_location in a variadic template function?
#include <iostream>
#include <utility>
#include <experimental/source_location>
/*
The C++20 feature std::source_location is used to capture information about the context in which a function is called.
*/
template <typename... Ts>
struct debug
{
debug(Ts&&... ts, const std::experimental::source_location& loc = std::experimental::source_location::current())
{
std::cout << loc.function_name() << " line " << loc.line() << ": ";
((std::cout << std::forward<Ts>(ts) << " "), ...);
std::cout << std::endl;
}
};
template <typename... Ts>
debug(Ts&&...) -> debug<Ts...>;
int main()
{
debug(5, 'A', 3.14f, "foo");
debug("bar", 123, 2.72);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment