Skip to content

Instantly share code, notes, and snippets.

@lichray
Created August 16, 2013 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lichray/6246780 to your computer and use it in GitHub Desktop.
Save lichray/6246780 to your computer and use it in GitHub Desktop.
Use variadic template instead of initializer_list to handle movable objects.
#include <type_traits>
#include <vector>
template <typename V, typename T1>
inline void back_pusher(V& v, T1 t1)
{
v.push_back(std::move(t1));
}
template <typename V, typename T1, typename... T2>
inline void back_pusher(V& v, T1 t1, T2... t2)
{
v.push_back(std::move(t1));
back_pusher(v, std::move(t2)...);
}
template <typename... T>
inline auto make_vector(T... t)
-> std::vector<std::common_type_t<T...>>
{
std::vector<std::common_type_t<T...>> v;
v.reserve(sizeof...(T));
back_pusher(v, std::move(t)...);
return v;
}
#include <iostream>
#include <string>
int main()
{
std::string s;
std::cin >> s;
auto v = make_vector("rvalue", s);
std::cout << v[1] << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment