I'm new with templates, specially with parameter pack and I wonder if I can get the first value from the pack.
For example the following code:
template <typename T, typename... Args>
bool register(Args... args) {
if (!Foo<T>(args..) {
assert(std::is_same_v<std::string, args...[0]>);
std::cerr << "Failed call Foo with " + args...[0] + "\n";
}
}
How do I really get the first value in args...
?
Worth to note that args..
. can contain different types (string, boolean, etc.)
You can use lambda to extract the first parameter:
template<typename T, typename... Args>
bool register(Args... args) {
if (!Foo<T>(args...)) {
auto& first = [](auto& first, auto&...) -> auto& { return first; }(args...);
static_assert(std::is_same_v<std::string,
std::remove_reference_t<decltype(first)>>);
std::cerr << "Failed call Foo with " + first + "\n";
}
}