Re: Variadic templates std::tuple
Juha Nieminen <nospam@thanks.invalid> wrote:
(Incidentally, one cool feature of this, something that very few C++
programmers yet understand, is that you can use the parameter pack
several times in an expression and append "..." at the end of it,
and it will work as expected, duplicating the entire expression and
substituting all the names as needed.)
To understand what I mean, here's a (rather contrived) example of
exactly that. (Note that this is not the kind of code that one would
normally write. It just demonstrates the use of variadic template
parameter packs in complex expressions.)
//-----------------------------------------------------------------------
void printPtrs() {}
template<typename Ptr, typename... Rest>
void printPtrs(const Ptr& ptr, const Rest&... rest)
{
std::cout << *ptr << std::endl;
printPtrs(rest...);
}
template<typename... Args>
void foo(Args&&... args)
{
printPtrs(std::shared_ptr<typename std::remove_reference<Args>::type>
(new typename std::remove_reference<Args>::type
(std::forward<Args>(args)))...);
}
//-----------------------------------------------------------------------
Notice how the last "..." in that code refers to the entirety of the
expression inside the printPtrs() call, and that there are three
references to 'Args' and one to 'args' inside it.
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---