Topic: Re: Variadic Templates Recursion Initializer?= =?windows-1252?Q? Lists.


Author: "Sergey S." <flex_ferrum@artberg.ru>
Date: Wed, 9 Dec 2009 17:14:31 CST
Raw View
On Dec 9, 9:08 pm, Kenshin <kenshin.himura.sakab...@gmail.com> wrote:
> Seems to me like the only way to write functions/classes taking
> variadic templates is by recursion. Is that true? Can the below work,
> for iteration?
>
> template<class   T>
> auto func(const T&   t){
>
>  std::initializer_list<?> ilist = {t  }; //what should go here <?>//
>   for(const auto& arg : ilist)
>        //use arg//
>
> }
No. Recusion is not only one way to deal with variadic templates.
There is at least three solutions for your example. First was
demonstrated here:
http://groups.google.com/group/comp.std.c++/msg/927ea19b5201a7fa

Another two solutions (samples):

// if you expect only integers
template<int... Nums>
int make_sum()
{
 auto list = {Nums...};

 int result = 0;
 return std::accumulate(list.begin(), list.end(), result);
}

// If you expect variadic params list of any type compatible each
other. For example, numeric types.
template<typename... Nums>
double make_sum(Nums ... nums)
{
 auto list = {(double)nums...};

 double result = 0;
 return std::accumulate(list.begin(), list.end(), result);
}

with following usage:
std::cout << make_sum<1, 2, 3, 4, 5, 6>() << std::endl; // first case
std::cout << make_sum(1, 2, 3, 4.234, 5, 6, 7.435, 8, 9, 10) <<
std::endl; // second case

---
Best regards,
Sergey


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Pavel Minaev <int19h@gmail.com>
Date: Tue, 15 Dec 2009 11:17:11 CST
Raw View
On Dec 9, 3:14 pm, "Sergey S." <flex_fer...@artberg.ru> wrote:
> template<int... Nums>
> int make_sum()
> {
>         auto list = {Nums...};

What would be the inferred type of "list" in this example?


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Sergey S." <flex_ferrum@artberg.ru>
Date: Tue, 15 Dec 2009 19:02:11 CST
Raw View
On Dec 15, 8:17 pm, Pavel Minaev <int...@gmail.com> wrote:
> On Dec 9, 3:14 pm, "Sergey S." <flex_fer...@artberg.ru> wrote:
>
> > template<int... Nums>
> > int make_sum()
> > {
> >         auto list = {Nums...};
>
> What would be the inferred type of "list" in this example?
Inferred type of "list" in this example is std::initializer_list<int>,
I think.

----------
Best regards,
Sergey


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]