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 

The name of a common C++ idiom

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





PostPosted: Thu Aug 19, 2004 12:17 pm    Post subject: The name of a common C++ idiom Reply with 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<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





PostPosted: Fri Aug 20, 2004 1:30 am    Post subject: Re: The name of a common C++ idiom Reply with quote



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





PostPosted: Fri Aug 20, 2004 1:40 am    Post subject: Re: The name of a common C++ idiom Reply with quote



"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
Quote:
matrix_classes;

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





PostPosted: Fri Aug 20, 2004 1:41 am    Post subject: Re: The name of a common C++ idiom Reply with quote

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
Quote:

{};

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





PostPosted: Fri Aug 20, 2004 10:02 am    Post subject: Re: The name of a common C++ idiom Reply with quote

[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





PostPosted: Sat Aug 21, 2004 3:25 am    Post subject: Re: The name of a common C++ idiom Reply with 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.

[ 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





PostPosted: Sat Aug 21, 2004 10:22 am    Post subject: Re: The name of a common C++ idiom Reply with quote

<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





PostPosted: Sun Aug 22, 2004 3:11 am    Post subject: Re: The name of a common C++ idiom Reply with quote

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





PostPosted: Tue Aug 24, 2004 7:54 pm    Post subject: Re: The name of a common C++ idiom Reply with quote

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





PostPosted: Tue Aug 24, 2004 10:38 pm    Post subject: Re: The name of a common C++ idiom Reply with quote

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





PostPosted: Tue Aug 24, 2004 10:44 pm    Post subject: Re: The name of a common C++ idiom Reply with quote

"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





PostPosted: Thu Sep 09, 2004 9:54 am    Post subject: Re: The name of a common C++ idiom Reply with quote

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
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.