| View previous topic :: View next topic |
| Author |
Message |
Dan Piponi Guest
|
Posted: Thu Aug 19, 2004 12:17 pm Post subject: The name of a common C++ idiom |
|
|
There are times when you need a template function (or set of
functions) to accept as argument a limited set of classes. One
strategy for this is simply to dispense with the template and derive
all of these classes from a single base class and have your functions
accept that base class as argument. But there are times when
inheritance is inappropriate. A alternative strategy is to define a
dummy 'base' template (let's call it Vector<>) and a set of dummy
'selector' class (e.g. DynamicVector, VectorSliceOfMatrix,
StaticVector) and instead of derivation implement specialisations with
the classes Vector<DynamicVector> and so on. That way we can no
implement function templates with signatures like template<class X>
f(Vector<X>) that only accept these specialized types as arguments.
So here's my very elementary question. Does this idiom have a name?
(and does it qualify to be called a 'design pattern'?)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Fri Aug 20, 2004 1:30 am Post subject: Re: The name of a common C++ idiom |
|
|
Dan Piponi wrote:
| Quote: | There are times when you need a template function (or set of
functions) to accept as argument a limited set of classes.
|
Why? What harm does it do to let people try to call the
template with any class they want?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Terje Slettebų Guest
|
Posted: Fri Aug 20, 2004 1:40 am Post subject: Re: The name of a common C++ idiom |
|
|
"Dan Piponi" <google (AT) sigfpe (DOT) com> wrote
| Quote: | There are times when you need a template function (or set of
functions) to accept as argument a limited set of classes. One
strategy for this is simply to dispense with the template and derive
all of these classes from a single base class and have your functions
accept that base class as argument. But there are times when
inheritance is inappropriate. A alternative strategy is to define a
dummy 'base' template (let's call it Vector<>) and a set of dummy
'selector' class (e.g. DynamicVector, VectorSliceOfMatrix,
StaticVector) and instead of derivation implement specialisations with
the classes Vector<DynamicVector> and so on. That way we can no
implement function templates with signatures like template<class X
f(Vector
So here's my very elementary question. Does this idiom have a name?
(and does it qualify to be called a 'design pattern'?)
|
You might do better (using Boost):
--- Start ---
#include <iostream>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/begin_end.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/utility/enable_if.hpp>
using namespace boost;
class DynamicVector {};
class VectorSliceOfMatrix {};
class StaticVector {};
class NotAVector {};
typedef mpl::vector<
DynamicVector,
VectorSliceOfMatrix,
StaticVector
template
typename disable_if<
is_same<
mpl::find
mpl::end<matrix_classes>::type
| Quote: |
::type
some_vector_operation(T t) |
{
std::cout << "some_vector_operation(T) calledn";
};
void some_vector_operation(...) // Catch-all; for illustration purposes
{
std::cout << "some_vecor_operation(...) calledn";
}
int main()
{
some_vector_operation(DynamicVector());
some_vector_operation(VectorSliceOfMatrix());
some_vector_operation(StaticVector());
some_vector_operation(NotAVector());
}
--- End ---
Output:
some_vector_operation(T) called
some_vector_operation(T) called
some_vector_operation(T) called
some_vecor_operation(...) called
This way, you won't have to define specialisations for all the matrix
classes. You may also constrain the template arguments on type attributes
(see the "Concepts" threads at comp.std.c++).
You may call this "constrained genericity", or "programming with concepts".
Regards,
Terje
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Terje Slettebų Guest
|
Posted: Fri Aug 20, 2004 1:41 am Post subject: Re: The name of a common C++ idiom |
|
|
| Quote: | From: "Terje Slettebų" <tslettebo (AT) hotmail (DOT) com
You might do better (using Boost):
template
typename disable_if
is_same
mpl::find
mpl::end<matrix_classes>::type
::type
some_vector_operation(T t)
{
std::cout << "some_vector_operation(T) calledn";
};
|
Naturally, you may encapsulate the selection mechanism, especially if you
need it more places:
template
struct is_vector :
mpl::not_<
is_same<
mpl::find
mpl::end<vector_classes>::type
template<class T>
typename enable_if<is_vector::type
some_vector_operation(T t)
{
std::cout << "some_vector_operation(T) calledn";
};
which makes it rather succinct, I think.
Regards,
Terje
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Fri Aug 20, 2004 10:02 am Post subject: Re: The name of a common C++ idiom |
|
|
[email]google (AT) sigfpe (DOT) com[/email] (Dan Piponi) writes:
| Quote: | There are times when you need a template function (or set of
functions) to accept as argument a limited set of classes. One
strategy for this is simply to dispense with the template and derive
all of these classes from a single base class and have your functions
accept that base class as argument. But there are times when
inheritance is inappropriate. A alternative strategy is to define a
dummy 'base' template (let's call it Vector<>) and a set of dummy
'selector' class (e.g. DynamicVector, VectorSliceOfMatrix,
StaticVector) and instead of derivation implement specialisations with
the classes Vector<DynamicVector> and so on. That way we can no
implement function templates with signatures like template<class X
f(Vector
So here's my very elementary question. Does this idiom have a name?
(and does it qualify to be called a 'design pattern'?)
|
Yes, this is one of the classic uses of the Curiously Recurring
Template Pattern (CRTP).
Some people get this confused and call it the "Barton & Nackman
trick", but don't let them fool you: B&N's trick involves generating
functions in base class templates as nested friends.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Narendranath.Nair@alcatel Guest
|
Posted: Sat Aug 21, 2004 3:25 am Post subject: Re: The name of a common C++ idiom |
|
|
Am not sure if this is getting off topic, but here goes:-
'functions' as you describe I think would not fit into the OO paradigm. In
OO we have Classes.
So, whether you want a method accepting a base class pointer or a template
method, the
choice, I would rather evaluate at classes and objects level. So, I don't
think if this classifies as a design pattern at all.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Terje Slettebų Guest
|
Posted: Sat Aug 21, 2004 10:22 am Post subject: Re: The name of a common C++ idiom |
|
|
<Narendranath.Nair (AT) alcatel (DOT) be> wrote
| Quote: | Am not sure if this is getting off topic, but here goes:-
'functions' as you describe I think would not fit into the OO paradigm. In
OO we have Classes.
So, whether you want a method accepting a base class pointer or a template
method, the
choice, I would rather evaluate at classes and objects level. So, I don't
think if this classifies as a design pattern at all.
|
Design patterns don't have to be OO (just because much of the GOF book had
OO patterns, and I think even if they were presented as OO patterns, some of
them might be implemented in a non-OO way).
See: http://hillside.net/patterns/definition.html
A quite common definition of a pattern is: "A pattern is a proven solution
to a problem in a context."
No "OO" in there, anywhere.
Regards,
Terje
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Sun Aug 22, 2004 3:11 am Post subject: Re: The name of a common C++ idiom |
|
|
On 20 Aug 2004 06:02:38 -0400, David Abrahams
<dave (AT) boost-consulting (DOT) com> wrote:
| Quote: | google (AT) sigfpe (DOT) com (Dan Piponi) writes:
There are times when you need a template function (or set of
functions) to accept as argument a limited set of classes. One
strategy for this is simply to dispense with the template and derive
all of these classes from a single base class and have your functions
accept that base class as argument. But there are times when
inheritance is inappropriate. A alternative strategy is to define a
dummy 'base' template (let's call it Vector<>) and a set of dummy
'selector' class (e.g. DynamicVector, VectorSliceOfMatrix,
StaticVector) and instead of derivation implement specialisations with
the classes Vector<DynamicVector> and so on. That way we can no
implement function templates with signatures like template<class X
f(Vector
So here's my very elementary question. Does this idiom have a name?
(and does it qualify to be called a 'design pattern'?)
Yes, this is one of the classic uses of the Curiously Recurring
Template Pattern (CRTP).
|
Take another closer look at the above. The two solutions presented use
only inheritance and only templates. The question requests a name for
the template only solution. The answer may be that using CRTP which
combines inheritance and templates may be a better solution which does
have a name.
As usual, a complete example might get a better answer.
| Quote: | Some people get this confused and call it the "Barton & Nackman
trick", but don't let them fool you: B&N's trick involves generating
functions in base class templates as nested friends.
|
Right. I think the word "base" may be a thinko. It is any class
template.
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Narendranath.Nair@alcatel Guest
|
Posted: Tue Aug 24, 2004 7:54 pm Post subject: Re: The name of a common C++ idiom |
|
|
Thanks for the point. Its indeed true that the "OO Design Patterns" from
GoF can be represented in an purely non oo way; e.g. I have seen an
implementation of the proxy design pattern using call backs and no OO at
all.
regards,
Naren
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Tue Aug 24, 2004 10:38 pm Post subject: Re: The name of a common C++ idiom |
|
|
| Quote: | Dan Piponi wrote:
There are times when you need a template function (or set of
functions) to accept as argument a limited set of classes.
|
Hyman Rosen <hyrosen (AT) mail (DOT) com> wrote
| Quote: | Why? What harm does it do to let people try to call the
template with any class they want?
|
No imagination?
Okay. I've got a template function (or set of functions) that
saves data into a database.
// Save employee to Employees table, automatically updates payroll
DBSave<Employee>(const Employee &);
// Save customer to Customers table, automatically updates mailing list
DBSave<Customer>(const Customer &);
etc.
If you somehow manage to call DBSave<Merchant>(const Merchant &); there
is going to be some big problems -- because our database doesn't have
a table for Merchants.
Personally I would write these as overloaded functions, not templates;
but that's as much taste as anything else.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Tue Aug 24, 2004 10:44 pm Post subject: Re: The name of a common C++ idiom |
|
|
"Terje Slettebų" <tslettebo (AT) hotmail (DOT) com> wrote
| Quote: | A quite common definition of a pattern is: "A pattern is a proven solution
to a problem in a context."
No "OO" in there, anywhere.
|
There is if you misspell it:
A pattern is a prooven solution to a problem in a context.
^^
:-)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dan Piponi Guest
|
Posted: Thu Sep 09, 2004 9:54 am Post subject: Re: The name of a common C++ idiom |
|
|
| Quote: | Why? What harm does it do to let people try to call the
template with any class they want?
|
That's easy to answer. To use the example I gave above I want to be
able to write my Vector<> objects to I/O streams and
template<class X> ostream operator<<(ostream &,const X &)
pollutes the namespace a little less than
template
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|