败犬日报 2025-09-24
败犬日报 2025-09-24
1. 参数包后面塞默认实参的问题 - 推导指引
众所周知 source_location 是默认实参传递的,在此基础上怎么实现参数包?其实只要写一个推导指引即可:
cpp
#include <format>
#include <print>
#include <source_location>
template <typename... Args>
struct Logger {
Logger([[maybe_unused]] Args&&... args,
std::source_location loc = std::source_location::current()) {
std::print("{}:{}:{}: ", loc.file_name(), loc.line(), loc.column());
(std::print("{}", std::forward<Args>(args)), ...);
std::println();
}
};
template <typename... Args>
Logger(Args...) -> Logger<Args...>;
int main() { Logger("Hello world", 123); }
日志 + 参数包 + source_location 的完整代码可以在这里获取 https://github.com/clice-io/clice/blob/main/include/Support/Logging.h。