The Artima Developer Community
Sponsored Link

Weblogs Forum
Inheriting from Template Parameters: A New Pattern?

8 replies on 1 page. Most recent reply: Jan 13, 2005 2:59 PM by Teng Lin

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Inheriting from Template Parameters: A New Pattern? (View in Weblogs)
Posted: Nov 12, 2004 9:58 AM
Reply to this message Reply
Summary
A technique which I use over and over again in C++ and Heron, which also can be used in Java, is inheriting from a template parameter. I am searching for a name for this pattern. One of the trouble spots for this pattern is lack of constructor inheritance in C++ and Java, but this can be worked around.
Advertisement
Okay I confess, I never finished the GoF ( short for the book by the Gang of Four - Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides ). I read it at a time that I was doing more writing of code than writing about code, as I am doing recently. I found that it restated techniques that I was already using, and giving them catchy names. Now I wish I got to the end, because I lack a good name for a pattern that I want to write about. I would like to call it wrapping, but apparently that is taken.

The pattern is very simple: inheriting from a template parameter. It is gaining in popularity, and I see it popping up more and more often in other people's code. One way I use the pattern is to define contract classes, which verify the contractual obligations of another class ( plug: as described in my article on Programming with Contracts in C++ to appear in a future edition of Doctor Dobbs Journal ), Another way I use the pattern, is to define extension classes which introduce operators and helper functions which can be defined in terms of a core set of

Some issues arise in C++ and Java due to constructors not being inherited, but in C++ at least -- and probably Java, though I don't know the syntax -- this can be worked around by parameterizing the constructor. This problem though doesn't occur in Heron because Heron classes automatically inherit constructors.

To work around the problem in C++ I currently use a parameterized constructors:

class Base {
public:
  Base(int x) {
    printf("initializing constructor with int\n");
  }
  Base(char x) {
    printf("initializing constructor with char\n");
  }
};

template
class Wrapper : public Super_T {
public:
  template 
  Wrapper(const T& x) : Super_T(x) {  }
};

int main(int argc, char* argv[])
{
  Wrapper a(5);
  Wrapper b('a');
  getchar();  
};
I am sure that this approach can be easily adopted to Java. If anyone has the name of the pattern, or has any comments, please share them!


David Roussel

Posts: 4
Nickname: diroussel
Registered: Jun, 2004

Re: Inheriting from Template Parameters: A New Pattern? Posted: Nov 12, 2004 5:08 PM
Reply to this message Reply
You could call it generic inheritance, or templated inheritance.

You won't be able to anything with the constructor of the argument class in java. Type erasure will make sure that we don't know which class we are dealing with at run time. The only constructor you can use is the default constructor.

Jonathan Turkanis

Posts: 3
Nickname: jdt
Registered: Nov, 2004

Re: Inheriting from Template Parameters: A New Pattern? Posted: Nov 13, 2004 2:20 AM
Reply to this message Reply
Hi!

Inheriting from a template parameter is one instance of the "Curiously Recurring Template Pattern" (CRTP) first described by James Coplien in the C++ Report, Feb. 1995.

It's one of my favorite patterns. It's used, for example, in the Boost Iterators library and the Boost Operators library.

I'll bet you thought there wasn't a name for it!

Jonathan

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: Inheriting from Template Parameters: A New Pattern? Posted: Nov 13, 2004 8:57 AM
Reply to this message Reply
Here's a link: http://c2.com/cgi/wiki?CuriouslyRecurringTemplate

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Inheriting from Template Parameters: A New Pattern? Posted: Nov 13, 2004 11:41 AM
Reply to this message Reply
Thanks for the link and the name! This is very helpful. I had heard the CRTP referenced before and didn't know what people were talking about.

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: Inheriting from Template Parameters: A New Pattern? Posted: Nov 13, 2004 12:09 PM
Reply to this message Reply
> Thanks for the link and the name! This is very helpful. I
> had heard the CRTP referenced before and didn't know what
> people were talking about.

It's a shame the article isn't posted some place (to my knowledge). My ten year collection of C++ Reports isn't as organized as I wish it was. Does the full article exist online any place?

Terje Slettebø

Posts: 205
Nickname: tslettebo
Registered: Jun, 2004

Re: Inheriting from Template Parameters: A New Pattern? Posted: Nov 17, 2004 6:39 AM
Reply to this message Reply
>Here's a link: http://c2.com/cgi/wiki?CuriouslyRecurringTemplate

Except that it's not quite the same. OPs example was:

template<class T>
class Wrapper : public T {...}

whereas in CRTP, the derived class is used as a template parameter of the base class (the derived class is "curiously recurring" as a base class template parameter):

class CRTP : Base<CRTP> {...}

From the link you gave: "It occurs where a base class is templated on the type of its derived class"

Regards,

Terje

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Inheriting from Template Parameters: A New Pattern? Posted: Nov 21, 2004 11:01 AM
Reply to this message Reply
I forgot to mention that Matthew Matthew Wilson, authour of Imperfect C++, describes a subcase of template parameter inheritance, caleld veneers, at http://synesis.com.au/resources/articles/cpp/veneers.pdf

Sorry Matthew for not mentioning veneers! ;-)

Teng Lin

Posts: 1
Nickname: hantek
Registered: Jan, 2005

Re: Inheriting from Template Parameters: A New Pattern? Posted: Jan 13, 2005 2:59 PM
Reply to this message Reply
It is called Mixin

Flat View: This topic has 8 replies on 1 page
Topic: I always get more done after everybody else leaves Previous Topic   Next Topic Topic: Secure Agility/Agile Security


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us