In order to really understand C++17 fold expression, I have written a function append for containers :
#include <iostream>
#include <vector>
#include <list>
// fold expression
template<typename T, typename U, template<typename> typename... Args>
inline void append_impl(std::vector<T>& v, Args<U>... args) noexcept
{
static_assert((std::is_constructible_v<T,U>));
std::cout << "append_impl version one " << std::endl;
(v.insert(std::end(v),
std::begin(args),
std::end (args)), ...);
}
//fold expression
template<typename T, typename... Args>
inline void append_impl(std::vector<T>& v, Args&&... args) noexcept
{
static_assert((std::is_constructible_v<T, Args&&> && ...));
std::cout << "append_impl version two " << std::endl;
(v.push_back(std::forward<Args>(args)), ...);
}
// fold expression
template<typename T, typename... Args>
inline void append(std::vector<T>& v, Args&&... args) noexcept
{
(append_impl(v, args), ...);
}
int main()
{
std::vector<int> a = {1,2};
std::vector<int> b = {3,4};
std::vector<int> c = {5,6};
std::list<int> d = {15,16};
append(a,b,c, std::vector<int>{8,9}, 10, 11, 12, 13, 14, d);
for(const auto& e : a)
{
std::cout << e << " ";
}
std::cout << std::endl;
return 0;
}
this works fine and give me the result :
append_impl version one
append_impl version one
append_impl version one
append_impl version two
append_impl version two
append_impl version two
append_impl version two
append_impl version two
append_impl version one
1 2 3 4 5 6 8 9 10 11 12 13 14 15 16
But I have a question from this code :
- In the first version of append_impl, args is pass by copy. I wanted to use universal references (in Scott Meyers sens) to avoid this, but
append_impl(std::vector<T>& v, Args<U>&&... args)
give me a compilation error.
rog.cc: In instantiation of 'void append_impl(std::vector<T>&, Args&& ...) [with T = int; Args = {std::vector<int, std::allocator<int> >&}]':
prog.cc:18:15: required from 'void append(std::vector<T>&, Args&& ...) [with T = int; Args = {std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >, int, int, int, int, int, std::__cxx11::list<int, std::allocator<int> >&}]'
prog.cc:29:59: required from here
prog.cc:10:3: error: static assertion failed
10 | static_assert((std::is_constructible_v<T, Args&&> && ...));
| ^~~~~~~~~~~~~
prog.cc:12:15: error: no matching function for call to 'std::vector<int>::push_back(std::vector<int>&)'
12 | (v.push_back(std::forward<Args>(args)), ...);
Why and what can I do to avoid copy ?