Re: explicit calling of member template constructor...
On Mar 25, 2:43 pm, Michael Doubez <michael.dou...@free.fr> wrote:
"I don't see" is what I wrote, didn't I ?
If I though there were no reason, I would have written as such.
Accepted. I wrongly assumed that you implied it by reading:
From the very beginning the question is biased [snip]
I nevertheless appreciate the response.
Example:
struct ArgHolder
{
template <class T>
explicit ArgHolder( const T* pValue )
: any_( pValue ){}
template <class T>
const T& subject() const
{
return *boost::any_cast<const T*>(any_);
}
private:
boost::any any_;
};
In your example, the type of the input seems itself part of the
contract.
Yes, one would think that an output that converts to
Base does not break that contract. Boost::any
does not allow that conversion though (at it turned
out it requires exact type match):
struct Base{}; struct Derived : Base{};
//...
boost::any d = new Derived();
Base* b = boost::any_cast<Base*>( d );
//...
//Fails - bad_cast.
Then, using a plain unchecked template is IMHO a design error: it is
too greedy.
I disagree considering what it is used for. It is used to
obtain configuration parameter information of arbitrary
type. The type is determined by the name of the option
and the position of the field. The reader of the
configuration information (that could be obtained in various
ways such as from a file or command line etc) has to know
what the representing type is (as he specifies the format
of the configuration file). If he gets that wrong the cast
fails.
In most cases the representing type is the actual type (and
the cast works), but in some cases the configuration can
vary depending on platform type (for example a different
derived factory may be returned in which case the base
factory is used). In these cases the configuration option name
of the application remains the same, but the information
used to create a platform specific type (thread/task as
example) may differ (see example).
The implementer needs to know what type the client (coder)
will be interested in, and he needs to specify that as the input
type (by using static_cast in this case). (BTW, I've used
static_cast from the beginning, but I just wondered whether
syntax existed to specify the template param for a member
template constructor).
ThreadCreator <---- WinThreadCreator
<---- PosixThreadCreator
//Client code... Knows the configuration file format.
const ThreadCreator& factory = config.getOption( "AppThreads" ).
arg( eThreadAdmin ).as<ThreadCreator>();
boost::scoped_ptr<Thread> adminThread = factory.create().
adminThread->execute();
//In above mentioned case type change is required during call to as...
unsigned portNumber =
config.getOption( "SomePort" ).arg( 0 ).as<unsigned>();
//Requires no type change...
It can also be noted that the client specifies the expected types to
be
parsed, it can therefore be expected (in my opinion) that during
the call to as<T> the client would know what T should be, else
he will get the appropriate runtime error.
I also realize that many of these ideas are already presented in
boost::program_options.
I hope this makes some sense.
Regards,
Werner