Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

value_type of variadic parameters

Tags:

c++

Can I do such parameter unpacking in C++? This code doesn't compile, but I think it is possible.

template <typename Container, typename... Args>
void foo(Container& container, Args&&... args){
    tuple<typename Container::value_type, typename Args::value_type...> values;
    ...
}
like image 403
Joseph Kirtman Avatar asked Jan 25 '23 14:01

Joseph Kirtman


1 Answers

Args&&... args is a forwarding reference. If you pass an lvalue to it, the corresponding type in Args will be deduced as an lvalue reference.

typename Args::value_type is only valid if Args is a class type, not a reference-to-class. Thus you need to strip the reference-ness from the types:

typename std::remove_reference_t<Args>::value_type...
like image 174
HolyBlackCat Avatar answered Feb 05 '23 07:02

HolyBlackCat