Created
October 17, 2019 11:35
-
-
Save jdiego/c2f70966aa66729eda07d215de7cbb5e to your computer and use it in GitHub Desktop.
How to use source_location in a variadic template function?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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