Topic: proposal: template argument ellipsis


Author: jarkko@videotron.ca ("Jarkko Lempiainen")
Date: Sat, 7 Feb 2004 21:56:30 +0000 (UTC)
Raw View
Hi,

I have sometimes faced the need to have variable
number of variable type arguments and to be able
to pass the list of arguments to another function
(in some very generic piece of code). For now I
have had to get around the lack of "template
argument ellipsis" with an ugly macro construct
(has its own set of problems) which overloads
the function, but it would be nice to have this
feature built-in to the language. The syntax
of the function declaration might look something
like this ("..." syntax might clash with the
existing ellipsis syntax, but I use it just to
make the point):

template<typename T_, ...>
T_ *myNew(...)
{
  return new(...);
}

And it could be called like this:

struct A
{
  A(float a_, const char *b_)  {/*...*/}
};

myNew<A>(1.0f, "foo");

The "template argument ellipsis" might be useful
for class templates as well:

template<typename T_, ...>
struct Tuple
{
  T_ head;
  Tuple<...> tail;
};

template<typename T_>
struct Tuple
{
  T_ head;
};

Has there been proposal for such a language
feature already?

Thanks, Jarkko

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: technews@kangaroologic.com ("Jonathan Turkanis")
Date: Sun, 8 Feb 2004 03:26:38 +0000 (UTC)
Raw View
""Jarkko Lempiainen"" <jarkko@videotron.ca> wrote in message
news:jS%Ub.95378$nL.1581395@weber.videotron.net...
> Hi,
>
> I have sometimes faced the need to have variable
> number of variable type arguments and to be able
> to pass the list of arguments to another function
> (in some very generic piece of code).



> Has there been proposal for such a language
> feature already?
>

Yes. See
http://www.osl.iu.edu/~jajarvi/publications/papers/vararg_templates_n1483.pdf.

Jonathan


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jarkko@videotron.ca ("Jarkko Lempiainen")
Date: Sun, 8 Feb 2004 07:55:20 +0000 (UTC)
Raw View
Ah, nice! It's kinda funny that they actually
propose use of the same syntax (use of "..." is
kinda intuitive I guess) and also use Tuple as an
example (: They got my vote, heh.

To further elaborate potential uses of this in the
standard, it would be useful atleast in STL
containers, where you could do something like:

  struct A {A(int, const char*) {/*...*/}};
  std::list<A> l;
  l.construct_back(1, "foo");

where std::list::construct_back() would construct
a new object at the end of the container, like
push_back(), but without actually requiring
copy-construction of the object. The motivation
for this would be:
  1) to avoid extra performance & memory overhead
     caused by copy-construction and temporal
     object.
  2) to allow client to omit copy-constructor
     implementation for types stored to some
     containers, which might be unnecessary
     and difficult for some complex types.

Jarkko


""Jonathan Turkanis"" <technews@kangaroologic.com> wrote in message
news:c03v4v$12nb0u$1@ID-216073.news.uni-berlin.de...
>
> ""Jarkko Lempiainen"" <jarkko@videotron.ca> wrote in message
> news:jS%Ub.95378$nL.1581395@weber.videotron.net...
> > Hi,
> >
> > I have sometimes faced the need to have variable
> > number of variable type arguments and to be able
> > to pass the list of arguments to another function
> > (in some very generic piece of code).
>
>
>
> > Has there been proposal for such a language
> > feature already?
> >
>
> Yes. See
>
http://www.osl.iu.edu/~jajarvi/publications/papers/vararg_templates_n1483.pdf.
>
> Jonathan
>
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]