Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Variadic te...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 5 Topic 9565 of 9831
Post > Topic >>

Re: Variadic templates. How can I implement this?

by Pavel Minaev <int19h@[EMAIL PROTECTED] > Apr 30, 2008 at 05:05 PM

On Apr 30, 8:37 pm, german diago <germandi...@[EMAIL PROTECTED]
> wrote:
> Hello. I'm trying to implement a variadic vector type in c++0x with
> the variadic templates proposal (in gcc-4.4).
>
> I tried with this, and I wonder why I can't do this.
>
> template <class...Values>
> class vector {
>     private:
>         //This is not legal
>         Values v...;
>      ....
>
> };
>
> I think this should be allowed and a way to access every v would be to
> give v, v2, v3 names to the unpacked parameter pack or something
> similar.

That would be rather brittle. Compiler shouldn't invent identifiers on
its own.

> Now I'm trying to do this, which is legal:
>
> template <class... Values> class vector;
>
> template <class Head, class...Tail> class vector<Head, Tail...> :
> vector<Head, Tail...>
> {
>     private:
>         Head head_;
>
> };
>
> But the problem is that as every head_ variable shadows its base head_
> variable, I don't know how to access them. It woul also be nice to be
> able to implement operator[] as a constexpr to be able to index
> the vector like v[0] and so on, but I think it's not possible.
> Anyway, I think the syntax to expand variables (first example) should
> be legal because doing what I'm doing in the second example is
> unnecessarily complicated.

A much simpler way to do what you want is to use std::tuple class - in
C++0x, it is extended to use variadic templates, so inside your class
you can declare a field like this:

std::tuple<Values...> v;

And then address individual elements of the tuple as usual:

cout << get<0>(v);
cin >> get<1>(v);
....


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 5 Posts in Topic:
Variadic templates. How can I implement this?
german diago <germandi  2008-04-30 10:37:57 
Re: Variadic templates. How can I implement this?
Pavel Minaev <int19h@[  2008-04-30 17:05:03 
Re: Variadic templates. How can I implement this?
Valeriu Catina <catina  2008-04-30 17:02:58 
Re: Variadic templates. How can I implement this?
=?ISO-8859-1?Q?=C9ric_Mal  2008-04-30 17:04:02 
Re: Variadic templates. How can I implement this?
Valeriu Catina <catina  2008-04-30 17:03:17 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 2:49:39 CDT 2008.