|
Boost : |
From: Jonathan Turkanis (technews_at_[hidden])
Date: 2004-08-22 21:37:22
"Alex Chovanec" <achovane_at_[hidden]> wrote in message
news:cgbhkt$599$1_at_sea.gmane.org...
>
> Ok, so I have a class Ptr_Wrapper that is a wrapper around a pointer
to an
> object of arbitrary type,
You mean Ptr_Wrapper is not a template? How does it remember the type
of object it contains?
> and it has a template constructor which accepts
> any kind of iterator. The constructor initializes the pointer member
with
> the address of the object that the iterator refers to.
>
> Now, I don't want is_convertible<T, Ptr_Wrapper> to be true for any
type T,
> so I want to use the enable_if library to eliminate specializations
of the
> template constructor where the type passed in is not a Readable
Lvalue
> iterator with the correct pointer type. But again, I can't just use
> is_readable_iterator<T> and is_lvalue_iterator<T>, because either
one will
> cause a compile error if T is not an iterator.
>
> I don't want there to be a compile error in this case, because
Ptr_Wrapper
> has another constructor which accepts a reference to an object an
> initializes the pointer member from that.
Possibly dumb question: why not have this second overload take a
pointer?
> I want this constructor to be
> chosen if a reference is passed instead of an iterator.
>
> Thus, in the template metaprogram that I use to decide whether or
not to
> include a particular template specialization of the constructor in
the set
> of candidate functions, I need to check that T is some kind of
iterator
> before applying is_readable_iterator<T> and is_lvalue_iterator<T>.
> Otherwise, I may get an unwanted compile error.
Okay, assuming the second overload has to take a reference, this does
sound like a case where you want to be able to treat almost any
possible type. Realistically, how often would:
template<typename T>
Ptr_Wrapper( const T&,
typename enable_if< is_incrementable<T> >::type* =
0,
typename enable_if< is_dereferenceable<T> >::type*
= 0 );
template<typename T>
Ptr_Wrapper( T&,
typename disable_if< is_incrementable<T> >::type* =
0,
typename disable_if< is_dereferenceable<T>
>::type* = 0 );
select the wrong overload?
If it's fairly rare, you could add a disambiguation mechanism:
Ptr_Wrapper pw = Ptr_Wrapper::from_object(x);
or
Ptr_Wrapper pw( disambiguation_func_with_better_name(x) );
Jonathan
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk