C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Overloading class templates on number of parameters

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Eric B
Guest





PostPosted: Fri Apr 28, 2006 4:06 pm    Post subject: Overloading class templates on number of parameters Reply with quote



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 ]
Back to top
Gene Bushuyev
Guest





PostPosted: Sat Apr 29, 2006 9:06 pm    Post subject: Re: Overloading class templates on number of parameters Reply with quote



"Eric B" <ebeyeler_g (AT) yahoo (DOT) com> wrote in message
news:1146227841.006692.306130 (AT) u72g2000cwu (DOT) googlegroups.com...
[...]
Quote:
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 ]
Back to top
Eric B
Guest





PostPosted: Mon May 01, 2006 2:06 pm    Post subject: Re: Overloading class templates on number of parameters Reply with quote



Gene Bushuyev wrote:
Quote:

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 ]
Back to top
Marek Vondrak
Guest





PostPosted: Mon May 01, 2006 11:06 pm    Post subject: Re: Overloading class templates on number of parameters Reply with quote

Quote:
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 ]
Back to top
Gene Bushuyev
Guest





PostPosted: Tue May 02, 2006 3:06 am    Post subject: Re: Overloading class templates on number of parameters Reply with quote

"Eric B" <ebeyeler_g (AT) yahoo (DOT) com> wrote in message
news:1146485473.107295.89600 (AT) j33g2000cwa (DOT) googlegroups.com...
Quote:

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 ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.