473,322 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Name for a common idiom

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
Jul 22 '05 #1
6 1353
Gareth Stockwell wrote:
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.
I don't think so, but then I would probably not know if there was one.

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


This is a syntax error. Should be:

class C : public typename Selector<Tag>::base_t

Jul 22 '05 #2

"Gareth Stockwell" <ga**************@hotmail.com> skrev i en meddelelse
news:17**************************@posting.google.c om...
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


Why not simply:

template<class base>
class C : public base
{
....
}

?

/Peter
Jul 22 '05 #3

"Peter Koch Larsen" <pk*****@mailme.dk> schrieb im Newsbeitrag
news:s8********************@news000.worldonline.dk ...

"Gareth Stockwell" <ga**************@hotmail.com> skrev i en meddelelse
news:17**************************@posting.google.c om...
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


Why not simply:

template<class base>
class C : public base
{
...
}


I thought exactly this one indirection level is the idiom. :-)

And if this is really the case, and the *tag classes do never carry
implementation,
we could discuss if the "switching" template variable should be really a
class or if a numeric type (together with some enum) would be a cleaner, but
perhapes not so flexible solution.
Regards
Michael


Jul 22 '05 #4
Gianni Mariani wrote in news:fb********************@speakeasy.net in
comp.lang.c++:
Gareth Stockwell wrote:
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.


I don't think so, but then I would probably not know if there was one.

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


This is a syntax error. Should be:

class C : public typename Selector<Tag>::base_t


I think this is the one place where typename is specificaly *not*
alowed, I've no idea why it is required after typedef though :).

mscv 7.1
error C2899: typename cannot be used outside a template declaration

g++ 3.2 and 3.3
test.cpp:12: no bases given following `:'
test.cpp:12: parse error before `typename'

bccx (EDG):
test.cpp:12: error expected an identifier

g++ 3.4 & 4.0 (prerelease)
test.cpp:12: error: keyword `typename' not allowed outside of templates

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5
>
This is a syntax error. Should be:

class C : public typename Selector<Tag>::base_t


I think the typename keyword is only required inside the body of the
class, not in its definition. I don't have the standard handy, so
I'll be pragmatic ... the above compiles fine with GCC (all versions
3.3.2 up to 3.4.2), and Intel C++ 8.0 :-)

Gareth
Jul 22 '05 #6
Gareth Stockwell wrote:
This is a syntax error. Should be:

class C : public typename Selector<Tag>::base_t

I think the typename keyword is only required inside the body of the
class, not in its definition. I don't have the standard handy, so
I'll be pragmatic ... the above compiles fine with GCC (all versions
3.3.2 up to 3.4.2), and Intel C++ 8.0 :-)


That makes sense. My bad.

G
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Philips | last post by:
I recently had the need to sort a large number of lists of lists, and wondered if an improvement to the Decorate-Sort-Undecorate idiom is in the works. Ideally, I would like to sort the list of...
3
by: David Morgenthaler | last post by:
In many of my scripts I've used the following idiom for accessing data files placed nearby: BASEDIR = os.path.dirname(__file__) .. .. .. fp = file(os.path.join(BASEDIR,"somefile.txt")) .. ..
7
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding Herb Sutter's idiom of implementation of operator= via nonthrowing swap() member function, to guarantee strict exception safety. The idea of the idiom is...
1
by: Rick | last post by:
I have the following stored procedure in SQL 2000 and would like to diplay the database name where data is drawn from. I'm using 4 databases db1, db2, db3, db4 which all have the same table...
6
by: Raterus | last post by:
Hello, Does anyone have any ideas how I can accomplish this using properties. I'm trying to create a property, that acts like a structure. Basically I'm trying to create a property that I could...
13
by: Winbatch | last post by:
Is there a function that I can use that given a few vectors which elements are common to all? for example vector<int> a; vector<int> b; a.push_back( 1 ); a.push_back( 2 ); a.push_back( 3 );
9
by: turnitup | last post by:
Dear All, I have RTFM'd until I am blue in the face and I am completely at a loss. I would be grateful for any help. I have a series of elements in my html called var1, var2, var3 etc I...
4
by: | last post by:
I have learned about compartmentalizing my code base using Class Libraries. I have my common code such as my ORM framework broken out into their own Class Libraries, which are referenced as...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.