Some time passed when I was doing C ++, but I am not familiar with templates.
I recently tried to write a class that wrapped std::vector<std::tuple<Types...>> . This class must have member functions, and I really need to be able to iterate through the tuple. In fact, if I can print each element of the tuple (in order), I could do whatever I need.
I found a solution using the cast, but I'm not sure about this, since it is based on an actor that I don't really like (plus, when I try to use static_cast , it does not compile).
My question is, is the following code correct, portable, is it a hack, and should I find another way to do this than to use this cast? Also, is this actor probably right to perform at runtime? Is there a way to do what I want without it?
std::ostream& operator<<(std::ostream& out, std::tuple<> const& tuple) { return out; // Nothing to do here } template<typename First, typename... Types> std::ostream& operator<<(std::ostream& out, std::tuple<First, Types...> const& tuple) { out << std::get<0>(tuple) << " "; // The cast that I don't like return out << (std::tuple<Types...>&) tuple; } int main() { auto tuple = std::make_tuple(1, 2.3, "Hello"); std::cout << tuple << std::endl; return 0; }
Thank you in advance for your answers.
c ++ templates c ++ 14 variadic-templates
tforgione
source share