Например:
Код: Выделить всё
#define ThisFile __FILE__ printIt;
};
Код: Выделить всё
#pragma once
#include
#include
#include
#include
struct Log
{
template
struct printIt
{
printIt(const char* format, Args&&...args, const std::source_location& loc)
{
const auto args_pack = std::tuple{ std::forward(args)...};
const auto msg = std::apply([&format](auto&...args) {
return std::vformat(format, std::make_format_args(args...));
}, args_pack);
std::println("{}:{} {}", loc.file_name(), loc.line(), msg);
}
};
template
printIt(const std::string_view, Args&&...) -> printIt;
template
static auto print(const std::string_view format, Args&...args,const std::source_location loc = std::source_location::current()) -> void
{
printIt(format, std::forward(args)..., loc);
}
};
Код: Выделить всё
#include "Log.hpp"
auto main() -> int
{
auto Logger = Log{};
const auto a = int{0};
const auto b = int{1};
Logger.print("{} {}", a, b);
}
Код: Выделить всё
#include "Log.hpp"
auto main() -> int
{
auto Logger = Log{};
const auto a = int{0};
const auto b = int{1};
Logger.print("%d %d", a, b);
}
Нет ли способа добиться того, что я пытаюсь сделать, без указания типов, которые входят в параметр шаблона? Или я могу помочь компилятору, подсказав ему, к чему он должен прийти? Я знаю, что руководства по дедукции не могут работать с функциями, иначе я бы попытался сделать это первым, но я застрял здесь и не знаю, куда идти дальше.
Ссылка на Godbolt: https://godbolt.org/z/sG7MTzP1a
Подробнее здесь: https://stackoverflow.com/questions/798 ... c-function
Мобильная версия