问题
I am trying to understand variadic templates in C++ and I am lost in the following example: Imagine a function foo(T, T, T, T) which takes variadic number of arguments of the same type T and converts them into a vector. Any idea how to implement one?
It should work like this
foo<int>(1,2,3,4) returns std::vector<int> x{1,2,3,4}
foo<double>(0.1,0.2,0.3) returns std::vector<double> x{0.1,0.2,0.3}
回答1:
If the T values as known at compile-time, you can pass they as template parameters and write something like
template<typename T, T ... Is>
void foo() {
std::vector<T> x { { Is.. } };
for( auto xx:x )
std::cout << xx << std::endl;
}
that is called
foo<int, 2, 3, 5, 7>();
Otherwise you have to pass they as arguments; something like
template <typename T, typename ... ARGS>
void foo (ARGS const & ... args) {
std::vector<T> x { { args... } };
for( auto xx:x )
std::cout << xx << std::endl;
}
that is called
foo<int>(2, 3, 5, 7);
or also (deducing the type T from the first argument)
template <typename T, typename ... ARGS>
void foo (T const & arg0, ARGS const & ... args) {
std::vector<T> x { { arg0, args... } };
for( auto xx:x )
std::cout << xx << std::endl;
}
that is called
foo(2, 3, 5, 7);
-- EDIT --
The OP write
It should work like this
foo<int>(1,2,3,4) returns std::vector<int> x{1,2,3,4}
foo<double>(0.1,0.2,0.3) returns std::vector<double> x{0.1,0.2,0.3}
So I suppose you can simply write
template <typename T, typename ... ARGS>
std::vector<T> foo (ARGS const & ... args)
{ return { args... }; }
回答2:
In foo, I create a vector with the exact capacity to hold all the parameters and then recursively handle each parameter by pushing it to the back of the vector, which is passed to a helper template function called append_to_vector.
#include<vector>
namespace test2
{
template<typename TN>
void append_to_vector(std::vector<TN>& outputvector, const TN& elem)
{
outputvector.push_back(elem);
};
template<typename T0, typename ...T1toN>
void append_to_vector(std::vector<T0>& outputvector, const T0& elem, T1toN... elems)
{
outputvector.push_back(elem);
append_to_vector(outputvector, elems...);
};
template<typename T, typename ...T0toN>
auto foo(const T0toN... elems)
{
std::vector<T> vec;
vec.reserve(sizeof...(elems));
append_to_vector(vec, elems...);
return vec;
};
}
int main()
{
std::vector<int> vec;
auto vec1 = test2::foo<int>(1,2,3,4);
auto vec2 = test2::foo<double>(0.1,0.2,0.3,0.4);
return 0;
}
回答3:
Way to do it in C++14:
#include <vector>
#include <initializer_list>
#include <iterator>
#include <algorithm>
#include <iostream>
template <typename T>
std::vector<T> function(std::initializer_list<T> elements)
{
return std::vector<T>(elements.begin(), elements.end());
}
int main()
{
std::vector<int> elements = function<int>({1, 2, 3});
std::copy(elements.begin(), elements.end(), std::ostream_iterator<const int>(std::cout, " "));
}
来源:https://stackoverflow.com/questions/47126273/converting-parameter-pack-into-a-vector