Topic: Overloading class templates on number of parameters


Author: "Eric B" <ebeyeler_g@yahoo.com>
Date: Mon, 1 May 2006 08:57:22 CST
Raw View
Gene Bushuyev wrote:
>
> Avoid the empty base classes by using partial specializations. E.g.
>
> template < class A, class B = void, class C = void>
> class Intermediate : public A, public B, public C {};
>
> template < class A, class B = void>
> class Intermediate<A,B,void> : public A, public B {};
>
> template < class A>
> class Intermediate<A,void,void> : public A {};
>
> It might require additional typing, but it avoids the overhead and it also adds
> the flexibility of implementing all those Intermediate classes properly.
>

Uh, can you actually derive from void?

Eric

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: none@none.com ("Marek Vondrak")
Date: Mon, 1 May 2006 22:43:22 GMT
Raw View
>> Avoid the empty base classes by using partial specializations. E.g.
>>
>> template < class A, class B = void, class C = void>
>> class Intermediate : public A, public B, public C {}; // [1]
>>
>> template < class A, class B = void>
>> class Intermediate<A,B,void> : public A, public B {};
>>
>> template < class A>
>> class Intermediate<A,void,void> : public A {}; // [2]
>>
>> It might require additional typing, but it avoids the overhead and it
>> also adds
>> the flexibility of implementing all those Intermediate classes properly.
>
> Uh, can you actually derive from void?

No. But, wait --- the code above does not derive from void.
Note that template specializations are independent of the primary
template definition. The fact that the primary template [1] inherits from B
(which is bound to "void" in [2]) is irrelevant to the specialization [2].

-- Marek





---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: spam@spamguard.com ("Gene Bushuyev")
Date: Tue, 2 May 2006 02:23:47 GMT
Raw View
"Eric B" <ebeyeler_g@yahoo.com> wrote in message
news:1146485473.107295.89600@j33g2000cwa.googlegroups.com...
>
> Gene Bushuyev wrote:
>>
>> Avoid the empty base classes by using partial specializations. E.g.
>>
>> template < class A, class B = void, class C = void>
>> class Intermediate : public A, public B, public C {};
>>
>> template < class A, class B = void>
>> class Intermediate<A,B,void> : public A, public B {};
>>
>> template < class A>
>> class Intermediate<A,void,void> : public A {};
>>
>> It might require additional typing, but it avoids the overhead and it also
>> adds
>> the flexibility of implementing all those Intermediate classes properly.
>>
>
> Uh, can you actually derive from void?
>


But if you look carefully at the code you will notice that it never attempts to
derive from void. Because partial specialiazations take care of the cases when
template parameter is void. So you can write:

class A;
class B;
class C;

Intermediate<A,B,C> i1; // primary template
Intermediate<A,B> i2; // same as Intermediate<A,B,void>, which is specialized
Intermediate<A> i3; // same as Intermediate<A,void,void>, which is also
specialized

P.S. An error slipped in the code I provided in the previous post. The second
specialization should have been:

template < class A, class B>
class Intermediate<A,B,void> : public A, public B {};

notice that default template arguments are not allowed for partial
specialization, neither they are needed - the primary template takes care of
that.

--
Gene Bushuyev (www.gbresearch.com)
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "Eric B" <ebeyeler_g@yahoo.com>
Date: Fri, 28 Apr 2006 10:55:23 CST
Raw View
In C++ Templates the Complete Guide by Vandevoorde and Josuttis,
section 13.12 talks about a possible future enhancement of overloaded a
class template based on the number of template parameters. Their
example was a tuple.
I recently came across a place where this concept would apply in my
code.
I have an intermediary class where I would like to say

template < class A >
Intermediate : public A {};

template < class A, class B >
Intermediate : public A, public B {};

tempate < class A, class B, class C >
Intermediate : public A, public B, public C {};

etc.

Instead, I need to say

struct nulltypeB {};
struct nulltypeC {};
template < class A, class B = nulltypeB, class C = nulltypeC >
Intermediate : public A, public B, public C {};

this is similar to how boost::tuple works in implementing "variable"
template parameters
maybe having the extra base classes in a single parameter instance
isn't an issue, I don't know... It adds extra space overhead to the
class (the EBO isn't applied in every case in practice). What is the
status of this, will this be a change in C++ 0x?

Eric

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "Gene Bushuyev" <spam@spamguard.com>
Date: 29 Apr 2006 20:30:06 GMT
Raw View
"Eric B" <ebeyeler_g@yahoo.com> wrote in message
news:1146227841.006692.306130@u72g2000cwu.googlegroups.com...
[...]
> I have an intermediary class where I would like to say
>
> template < class A >
> Intermediate : public A {};
>
> template < class A, class B >
> Intermediate : public A, public B {};
>
> tempate < class A, class B, class C >
> Intermediate : public A, public B, public C {};
>
> etc.
>
> Instead, I need to say
>
> struct nulltypeB {};
> struct nulltypeC {};
> template < class A, class B = nulltypeB, class C = nulltypeC >
> Intermediate : public A, public B, public C {};
>
> this is similar to how boost::tuple works in implementing "variable"
> template parameters
> maybe having the extra base classes in a single parameter instance
> isn't an issue, I don't know... It adds extra space overhead to the
> class (the EBO isn't applied in every case in practice). What is the
> status of this, will this be a change in C++ 0x?


Avoid the empty base classes by using partial specializations. E.g.

template < class A, class B = void, class C = void>
class Intermediate : public A, public B, public C {};

template < class A, class B = void>
class Intermediate<A,B,void> : public A, public B {};

template < class A>
class Intermediate<A,void,void> : public A {};

It might require additional typing, but it avoids the overhead and it also adds
the flexibility of implementing all those Intermediate classes properly.

--
Gene Bushuyev (www.gbresearch.com)
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy

---
[ 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.comeaucomputing.com/csc/faq.html                      ]