 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Nicola Musatti Guest
|
Posted: Fri Aug 27, 2004 2:52 am Post subject: Compile time constructor selection |
|
|
Hallo, keyboard-pals.
I have a class template that takes a policy class as parameter:
template <typename PolicyT> class Implementation : public PolicyT {
// ...
};
The Implementation class is not directly instantiated, but is used as
a base class:
class SomePolicy {
// ...
};
class Configuration : public Implementation<SomePolicy>
{
// ...
};
My problem is the following: the constructors of different policies
may differ in the number and type of their parameters.
Implementation's constructor(s) only forward their argument(s) to the
policies' constructors. The first solution that comes to my mind is to
provide n templated constructors, taking from 1 to n parameters, as in
template <typename A> Implementation(A a) : PolicyT(A) {}
template <typename A, typename B> Implementation(A a,B b) :
PolicyT(a,b) {}
etc.
Is there a more clever way?
Thanks,
Nicola Musatti
[ 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 27, 2004 11:05 am Post subject: Re: Compile time constructor selection |
|
|
[email]Nicola.Musatti (AT) ObjectWay (DOT) it[/email] (Nicola Musatti) writes:
| Quote: | Hallo, keyboard-pals.
I have a class template that takes a policy class as parameter:
template <typename PolicyT> class Implementation : public PolicyT {
// ...
};
The Implementation class is not directly instantiated, but is used as
a base class:
class SomePolicy {
// ...
};
class Configuration : public Implementation<SomePolicy
{
// ...
};
My problem is the following: the constructors of different policies
may differ in the number and type of their parameters.
Implementation's constructor(s) only forward their argument(s) to
the policies' constructors. The first solution that comes to my mind
is to provide n templated constructors, taking from 1 to n
parameters, as in
template
template <typename A, typename B> Implementation(A a,B b) :
PolicyT(a,b) {}
etc.
Is there a more clever way?
|
No; at some point you need to do that. And unfortunately, you'll run
into the forwarding problem if you want to pass rvalues and lvalues
transparently. The one trick that's available to you, if you have
many layers to pass the parameters through, is to pack them up into a
tuple and only unpack them at the innermost layer.
'Course, there could be something I don't know about...
....naaah! ;-)
HTH,
--
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 |
|
 |
Alf P. Steinbach Guest
|
Posted: Fri Aug 27, 2004 11:10 am Post subject: Re: Compile time constructor selection |
|
|
* Nicola Musatti:
| Quote: | Hallo, keyboard-pals.
I have a class template that takes a policy class as parameter:
template <typename PolicyT> class Implementation : public PolicyT {
// ...
};
The Implementation class is not directly instantiated, but is used as
a base class:
class SomePolicy {
// ...
};
class Configuration : public Implementation<SomePolicy
{
// ...
};
My problem is the following: the constructors of different policies
may differ in the number and type of their parameters.
Implementation's constructor(s) only forward their argument(s) to the
policies' constructors. The first solution that comes to my mind is to
provide n templated constructors, taking from 1 to n parameters, as in
template
template <typename A, typename B> Implementation(A a,B b) :
PolicyT(a,b) {}
etc.
Is there a more clever way?
|
How about just a single parameter (I mean, is that more clever or not?).
A single parameter can contain anything: it really is any number of
parameters, in one, and it can even support default values...
I think I'd pass it by reference to const.
To support parameterless SomePolicy constructors you can define a
NoArguments class to use as actual param, and that's what I'd do.
Or you can alternatively support no-parameter constructors directly
instead of leveraging the single-parameter-as-multi-parameter mechanism
(wow! there's an opportunity for a _really clever_ acronym! ) ).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Aug 27, 2004 11:11 am Post subject: Re: Compile time constructor selection |
|
|
"Nicola Musatti" <Nicola.Musatti (AT) ObjectWay (DOT) it> wrote...
| Quote: | Hallo, keyboard-pals.
I have a class template that takes a policy class as parameter:
template <typename PolicyT> class Implementation : public PolicyT {
// ...
};
The Implementation class is not directly instantiated, but is used as
a base class:
class SomePolicy {
// ...
};
class Configuration : public Implementation<SomePolicy
{
// ...
};
My problem is the following: the constructors of different policies
may differ in the number and type of their parameters.
Implementation's constructor(s) only forward their argument(s) to the
policies' constructors. The first solution that comes to my mind is to
provide n templated constructors, taking from 1 to n parameters, as in
template
template <typename A, typename B> Implementation(A a,B b) :
PolicyT(a,b) {}
etc.
Is there a more clever way?
|
I think a more clever way would be to have policies to have the same
number and types of arguments in their constructors.
Victor
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
anton muhin Guest
|
Posted: Fri Aug 27, 2004 1:00 pm Post subject: Re: Compile time constructor selection |
|
|
Nicola Musatti wrote:
| Quote: | Hallo, keyboard-pals.
I have a class template that takes a policy class as parameter:
template <typename PolicyT> class Implementation : public PolicyT {
// ...
};
The Implementation class is not directly instantiated, but is used as
a base class:
class SomePolicy {
// ...
};
class Configuration : public Implementation<SomePolicy
{
// ...
};
My problem is the following: the constructors of different policies
may differ in the number and type of their parameters.
Implementation's constructor(s) only forward their argument(s) to the
policies' constructors. The first solution that comes to my mind is to
provide n templated constructors, taking from 1 to n parameters, as in
template
template <typename A, typename B> Implementation(A a,B b) :
PolicyT(a,b) {}
etc.
Is there a more clever way?
Thanks,
Nicola Musatti
|
I met the same problem too and the best decision I was able to find out
was to introduce the structure that holds ctor's parameters.
with the best regards,
anton.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Fri Aug 27, 2004 1:04 pm Post subject: Re: Compile time constructor selection |
|
|
Nicola Musatti wrote:
[]
| Quote: | My problem is the following: the constructors of different policies
may differ in the number and type of their parameters.
Implementation's constructor(s) only forward their argument(s) to the
policies' constructors. The first solution that comes to my mind is to
provide n templated constructors, taking from 1 to n parameters, as in
template <typename A> Implementation(A a) : PolicyT(A) {}
template <typename A, typename B> Implementation(A a,B b) :
PolicyT(a,b) {}
etc.
Is there a more clever way?
|
I would consider making policies to take exactly one parameter - a (boost::)tuple. That way you would only have to provide one forwarding constructor:
template<class H, class T>
Implementation(boost::cons<H, T> const& t) : PolicyT(t) {}
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Mon Aug 30, 2004 11:57 pm Post subject: Re: Compile time constructor selection |
|
|
Hallo,
thanks everybody for your answers.
I think I'll follow Maxim's suggestion and only provide a single
templated constructor taking a boost::tuple as argument.
The default and single argument constructors in my Configuration and
Policy classes will become a little more convoluted, but at least my
Implementation class template will not have to be modified to support
increasing number of arguments.
Making policies and Implementation's constructors take the same
arguments is not an option because this would lead to either make
Implementation depend on policy specific information or to two phase
initialization, which I consider not acceptable as policy constructor
parameters are supposed to be essential to their behaviour.
Thanks again,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Tue Aug 31, 2004 12:06 am Post subject: Re: Compile time constructor selection |
|
|
[email]Nicola.Musatti (AT) ObjectWay (DOT) it[/email] (Nicola Musatti) wrote in message
news:<a327cf48.0408260109.53a56d5a (AT) posting (DOT) google.com>...
| Quote: | I have a class template that takes a policy class as parameter:
template <typename PolicyT> class Implementation : public PolicyT {
// ...
};
The Implementation class is not directly instantiated, but is used as
a base class:
class SomePolicy {
// ...
};
class Configuration : public Implementation<SomePolicy
{
// ...
};
My problem is the following: the constructors of different policies
may differ in the number and type of their parameters.
Implementation's constructor(s) only forward their argument(s) to the
policies' constructors. The first solution that comes to my mind is to
provide n templated constructors, taking from 1 to n parameters, as in
template
template <typename A, typename B> Implementation(A a,B b) :
PolicyT(a,b) {}
etc.
Is there a more clever way?
|
Why not construct the policy in the derived class, and use a copy
contructor for it in the template? The template class would only have a
constructor:
template< typename PolicyT >
Implementation::Implementation(
PolicyT const& policy )
: PolicyT( policy )
{
}
Or is there something stupid I'm overlooking.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Tue Aug 31, 2004 7:48 pm Post subject: Re: Compile time constructor selection |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote in message news:<d6652001.0408300114.210d5f4b (AT) posting (DOT) google.com>...
[...]
| Quote: | Why not construct the policy in the derived class, and use a copy
contructor for it in the template? The template class would only have a
constructor:
template< typename PolicyT
Implementation::Implementation(
PolicyT const& policy )
: PolicyT( policy )
{
}
Or is there something stupid I'm overlooking.
|
As far as I can tell it should work and it is certainly a simpler
approach than using Boost.Tuple or some similar mechanism. However I
can't get rid of the feeling I'd be misusing my policy class...
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Wed Sep 01, 2004 10:13 am Post subject: Re: Compile time constructor selection |
|
|
<kanze (AT) gabi-soft (DOT) fr> wrote:
[]
| Quote: | Why not construct the policy in the derived class, and use a copy
contructor for it in the template? The template class would only have a
constructor:
template< typename PolicyT
Implementation::Implementation(
PolicyT const& policy )
: PolicyT( policy )
{
}
Or is there something stupid I'm overlooking.
|
It's a good general solution. But if the policy class is not copy constructable, you might end up implementing a copy constructor with move
semantics in addition to the constructor in your policy class, while with tuple it is a simple initialization.
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Sep 01, 2004 8:55 pm Post subject: Re: Compile time constructor selection |
|
|
[email]Nicola.Musatti (AT) ObjectWay (DOT) it[/email] (Nicola Musatti) wrote in message
news:<a327cf48.0408310441.58eaf7dc (AT) posting (DOT) google.com>...
| Quote: | kanze (AT) gabi-soft (DOT) fr wrote in message
news:<d6652001.0408300114.210d5f4b (AT) posting (DOT) google.com>...
[...]
Why not construct the policy in the derived class, and use a copy
contructor for it in the template? The template class would only
have a constructor:
template< typename PolicyT
Implementation::Implementation(
PolicyT const& policy )
: PolicyT( policy )
{
}
Or is there something stupid I'm overlooking.
As far as I can tell it should work and it is certainly a simpler
approach than using Boost.Tuple or some similar mechanism. However I
can't get rid of the feeling I'd be misusing my policy class...
|
Is it copiable, or isn't it:-)? If it is copiable, then copying it
isn't missuse. If it isn't copiable, then the code shouldn't compile,
since you will have declared the copy constructor private (or done
something else to prevent copying, like inheriting from
boost::noncopyable).
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|