I like c++11 variadic templates, so I often write some little codes with it.
See this example:
#include <cstdio>
#include <type_traits>
#include <vector>
template< typename ... T >
auto make_vector(T ... t ) -> std::vector< typename std::common_type<T...>::type >
{
std::vector< typename std::common_type<T...>::type > v;
v.reserve( sizeof...(T) );
using list = int[];
(void)list{ 0, ( (void)v.push_back(std::move(t)) ,0)... };
// |/ / / /
// --------
// \-- How are evaluated v.push_back()s, sequentially or arbitrary ?
return v;
}
int main()
{
auto v = make_vector(2, 3.0, 'a', 7UL );
for(auto e : v )
printf("%.2lf ", e);
printf("\n");
}
Q: Is evaluation order of initialization of array sequentially or arbitrary (or implementation defined, undefined behavior) ?
If make_vector
is wrong, how me fix its?
push_back
? Why not construct the vector directly?return {std::forward<typename std::common_type<T...>::type>(t)...};
– Resh