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 

Name for a common idiom

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Gareth Stockwell
Guest





PostPosted: Sun Oct 24, 2004 4:13 am    Post subject: Name for a common idiom Reply with quote



I've been using an technique whereby a tag used as a parameter to a
template class C determines which of several potential base classes
(A,B) C inherits from. What I want to know is whether there is an
accepted name for this idiom.

To illustrate the point, here is some code which demonstrates it in
action...

// Selector classes - these are typically just empty structs
struct A_Tag { };
struct B_Tag { };

// Potential base classes
class A { /* ... */ };
class B { /* ... */ };

// Definition of the class which selects the appropriate base for C
template<class Tag> struct Selector { };

// Specialisations of the selector map tags onto base classes
template<> struct Selector<A_Tag> { typedef A base_t; };
template<> struct Selector<B_Tag> { typedef B base_t; };

// OK, here is the class which we actually instantiate
template<class Tag>
class C : public Selector<Tag>::base_t
{ /* ... */ };

// And now the instantiations...
C<A_Tag> c_inheriting_from_A;
C<B_Tag> c_inheriting_from_B;


Gareth

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Allen Yao
Guest





PostPosted: Tue Oct 26, 2004 4:24 am    Post subject: Re: Name for a common idiom Reply with quote



Why not just:

template <typename T>
class C: public T
{ ... };

Quote:
I've been using an technique whereby a tag used as a parameter to a
template class C determines which of several potential base classes
(A,B) C inherits from. What I want to know is whether there is an
accepted name for this idiom.

To illustrate the point, here is some code which demonstrates it in
action...

// Selector classes - these are typically just empty structs
struct A_Tag { };
struct B_Tag { };

// Potential base classes
class A { /* ... */ };
class B { /* ... */ };

// Definition of the class which selects the appropriate base for C
template<class Tag> struct Selector { };

// Specialisations of the selector map tags onto base classes
template<> struct Selector<A_Tag> { typedef A base_t; };
template<> struct Selector<B_Tag> { typedef B base_t; };

// OK, here is the class which we actually instantiate
template<class Tag
class C : public Selector { /* ... */ };

// And now the instantiations...
C<A_Tag> c_inheriting_from_A;
C<B_Tag> c_inheriting_from_B;


Gareth



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

Back to top
Jonathan Turkanis
Guest





PostPosted: Tue Oct 26, 2004 4:24 am    Post subject: Re: Name for a common idiom Reply with quote




"Gareth Stockwell" <gareth_stockwell (AT) hotmail (DOT) com> wrote

Quote:
I've been using an technique whereby a tag used as a parameter to a
template class C determines which of several potential base classes
(A,B) C inherits from.

Why use a tag? Why not let the base class represent itself?

Quote:
// Definition of the class which selects the appropriate base for C
template<class Tag> struct Selector { };

// Specialisations of the selector map tags onto base classes
template<> struct Selector<A_Tag> { typedef A base_t; };
template<> struct Selector<B_Tag> { typedef B base_t; };

// OK, here is the class which we actually instantiate
template<class Tag
class C : public Selector { /* ... */ };

Why not:

template<typename Base>
class C : Base { /* ... */ };

?

Quote:
// And now the instantiations...
C<A_Tag> c_inheriting_from_A;
C<B_Tag> c_inheriting_from_B;

This becomes:

C<A> // C inheriting from A
C<B> // C inheriting from B

If for some reason you need an extra level of indirection, inheriting from
boost:mpl::if_ or boost:mpl::eval_if is much more flexible.

Quote:

Gareth


Jonathan



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

Back to top
Gareth Stockwell
Guest





PostPosted: Wed Oct 27, 2004 1:02 am    Post subject: Re: Name for a common idiom Reply with quote

"Allen Yao" <yaozhen (AT) ustc (DOT) edu> wrote

Quote:
Why not just:

template <typename T
class C: public T
{ ... };



OK, my example was a trivial one, but typically the system is more
complicated. For example, say we're writing a graph class, which
consists of a number of components e.g. vertex container, edge
container etc. I use the Composite pattern, inheriting Graph from the
appropriate container objects - but I don't want the user to have to
worry about the details of how those containers are implemented.

In other words, I want to be able to write
Graph<
my_vertex_class, my_edge_class,
VertexListTag, AdjacencyMatrixTag
Quote:


rather than
Graph<
my_vertex_class, my_edge_class,
VertexListContainer AdjacencyMatrixContainer<my_edge_class>
Quote:


The user shouldn't *need* to know the name of the base class, or the
parameters used to correctly define that base class. This allows me
to decouple the definition of the Graph template from that of its
component parts.

Gareth

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


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) 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.