Boost logo

Boost Users :

Subject: Re: [Boost-users] enable_if and templated operator() of member of class template
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2012-03-01 11:48:06


On Thu, 1 Mar 2012, MM wrote:

> Hello,
>
> I am using the enable_if construct to disable an operator() template member
> function of a templated variant visitor:
>
>
> template <typename iter_t>
> class iterator_compare_visitor_ : public boost::static_visitor<bool> {
> public:
> iterator_compare_visitor_(const iter_t& to_compare_to)
> : to_compare_to_(to_compare_to)
> {}
>
> template <typename variant_iter_t>
> typename std::enable_if< boost::mpl::contains<const_iterators_t,
> iter_t>::value, bool >::type
> operator()(const variant_iter_t& it) const
> {
> return std::is_same<variant_iter_t, iter_t>::value &&
> it==to_compare_to_;
> }
>
> template <typename variant_iter_t>
> typename std::enable_if< boost::mpl::contains<const_reverse_iterators_t,
> iter_t>::value, bool >::type
> operator()(const variant_iter_t& it) const
> {
> return std::is_same<variant_iter_t, iter_t>::value &&
> it==to_compare_to_.base();
> }
>
> private:
> const iter_t& to_compare_to_;
> };
>
>
> The compilation fails in msvc2010 with:
>
> error C2039: 'type' : is not a member of 'std::tr1::enable_if<_Test,_Type>'
> 1> with
> 1> [
> 1> _Test=false,
> 1> _Type=bool
> 1> ]

The main issue here is that the condition in enable_if does not depend on
the template parameter to operator(); see the version in
http://codepad.org/6AORo7tU, in which I've used boost::enable_if and added
some tests. One fix is to make the enable_if condition dependent, as in
http://codepad.org/916Tl6W8 (there were a number of other issues in the
code; note that the is_same<variant_iter_t, iter_t> condition needs to be
in the enable_if since the body of the function will not always compile
when that is false). Please see if that last paste works for you.

-- Jeremiah Willcock


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net