在您的情况下更简单似乎将您的功能更改为:
template <typename T, typename Arg, typename... Args>
bool register(Arg arg, Args... args) {
if (!Foo<T>(arg, args...) {
assert(std::is_same_v<std::string, Arg>);
std::cerr << "Failed call Foo with " + arg + "\n";
}
}
从断言中,甚至
template <typename T, typename... Args>
bool register(const std::string& s, Args... args) {
if (!Foo<T>(s, args...) {
std::cerr << "Failed call Foo with " + s + "\n";
}
}
else<tuple>
提供了一些有用的工具:
template <typename T, typename Arg, typename... Args>
bool register(Args... args) {
if (!Foo<T>(args...) {
assert(std::is_same_v<std::string,
std::tuple_element_t<0, std::tuple<Args...>>);
std::cerr << "Failed call Foo with "
+ std::get<0>(std::tie(args...)) + "\n";
}
}