Topic: sizeof pack expansion as an argument of a class template partial specialization.


Author: snk_kid <korcanh@googlemail.com>
Date: Wed, 25 Nov 2009 11:44:04 CST
Raw View
Just to note that I know I can achieve the following by using a helper
meta-function like so:

template < const size_t N, typename... Types >
struct foo_helper
{
     static const size_t Index = sizeof...(Types) - N;
    // ....
};

template < typename... Types >
struct foo_helper< 0, Types... > { ... };

template < typename ... Types >
struct foo : foo_helper<sizeof... (Types), Types...> {...};

However there doesn't seem to be any reason to not allow for what I
mentioned in the first post.

--
[ 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: CornedBee <wasti.redl@gmx.net>
Date: Fri, 27 Nov 2009 17:57:34 CST
Raw View
On Nov 25, 12:42 am, snk_kid <korc...@googlemail.com> wrote:
> Is the following illegal in the current working draft?
>
> template < size_t Index, typename...Types >
> struct foo { /* ... */ };
>
> template < typename...Types >
> struct foo<sizeof... (Types), Types...> { /* ... */ };
>
> typedef foo<0, ...> result_type;

The variable argument lists confuse the issue, because I think you're
misunderstanding partial specialization. I've failed to come up with
the standard reference, but basically, a partial specialization does a
pattern matching against the template arguments to use a different
definition - but the important thing is, any template argument list
supplied to the template must be valid for the primary template. The
specializations only come into play after that.
In other words, foo<int, float>, which seems what you want to write,
is not valid, because the foo template expects a non-type parameter of
type size_t in the first position. The partial specialization doesn't
change this.

Now, if your partial specialization is valid (I have no idea if it
is), this only means that if I write foo<1, int, float>, the primary
definition is chosen, whereas if I write foo<2, int, float>, pattern
matching resolves sizeof...(Types) to 2 and sees that the partial
specialization matches; this definition is chosen instead.


--
[ 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: snk_kid <korcanh@googlemail.com>
Date: Sun, 29 Nov 2009 21:25:04 CST
Raw View
>
> The variable argument lists confuse the issue, because I think you're
> misunderstanding partial specialization. I've failed to come up with
> the standard reference, but basically, a partial specialization does a
> pattern matching against the template arguments to use a different
> definition - but the important thing is, any template argument list
> supplied to the template must be valid for the primary template.
>

Yes I'm aware of that already.

> The specializations only come into play after that.
> In other words, foo<int, float>, which seems what you want to write,
> is not valid, because the foo template expects a non-type parameter of
> type size_t in the first position. The partial specialization doesn't
> change this.

Where have I mentioned that I want anything like a foo<int, float>?
I'm talking about variadic templates here where "sizeof... (Types)"
gives the size of the type list, that would be valid template argument
for non-type parameter.

>
> Now, if your partial specialization is valid (I have no idea if it
> is), this only means that if I write foo<1, int, float>, the primary
> definition is chosen, whereas if I write foo<2, int, float>, pattern
> matching resolves sizeof...(Types) to 2 and sees that the partial
> specialization matches; this definition is chosen instead.
>

Now I'm confused with what you just said earlier, I can't tell if you
understand that I'm talking about variadic templates & you understand
them or not.

--
[ 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: CornedBee <wasti.redl@gmx.net>
Date: Mon, 30 Nov 2009 12:22:03 CST
Raw View
On Nov 30, 4:25 am, snk_kid <korc...@googlemail.com> wrote:
>
>
> > Now, if your partial specialization is valid (I have no idea if it
> > is), this only means that if I write foo<1, int, float>, the primary
> > definition is chosen, whereas if I write foo<2, int, float>, pattern
> > matching resolves sizeof...(Types) to 2 and sees that the partial
> > specialization matches; this definition is chosen instead.
>
> Now I'm confused with what you just said earlier, I can't tell if you
> understand that I'm talking about variadic templates & you understand
> them or not.

Yes, I understand that you're talking about variadic templates and I
understand them.
What I apparently don't understand is what you want to do. What is the
code you want to write as a client of the example you presented above,
and what is it supposed to achieve? I was under the impression that
you want to write
foo<int, float>
and it should have the same effect as foo<2, int, float>.
Alternatively, you might want to have the instantiations foo<1, int,
float> and foo<2, int, float> and you want to special-case the
implementation of the latter, because the integral argument is the
same as the number of elements in the parameter pack.

What really confuses me is your second post, where you show a
workaround. However, this workaround presents the client with a foo
that has a different parameter list, so I don't see the equivalency.

Sebastian


--
[ 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                      ]