enable_if and templated operator() of member of class template

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

"MM"
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
::value, bool >::type operator()(const variant_iter_t& it) const { return std::is_same ::value && it==to_compare_to_; } template <typename variant_iter_t> typename std::enable_if< boost::mpl::contains
::value, bool >::type operator()(const variant_iter_t& it) const { return std::is_same ::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> ]
in this case, iter_t is not contained in const_reverse_iterators_t and so the 2nd operator() should not be enabled.
Rds,
MM
What you are trying cannot work as this is not a valid way to overload operator(). Consider using enable_if as an argument to your functions: http://codepad.org/nduBfhT6 As for the actual error: It looks like your compiler is using std::tr1::enable_if even if you specify `std::enable_if`. Those should be equivalent. You might want to use boost::enable_if. Also, please try to provide snippets that don't require modifications to show the bug (e.g. includes). HTH, Philipp Moeller

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
::value, bool >::type operator()(const variant_iter_t& it) const { return std::is_same ::value && it==to_compare_to_; } template <typename variant_iter_t> typename std::enable_if< boost::mpl::contains
::value, bool >::type operator()(const variant_iter_t& it) const { return std::is_same ::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

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Jeremiah Willcock Sent: 01 March 2012 16:48 To: boost-users@lists.boost.org Subject: Re: [Boost-users] enable_if and templated operator() of member of class template
On Thu, 1 Mar 2012, MM wrote: 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
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
Thank you, http://codepad.org/916Tl6W8 compiled. Using std::enable_if as
well. (msvc10 uses their TR1 impl).
I need an extra check for the 2nd operator() of the visitor, that
variant_iter_t is actually identical to iter_t::Iterator embedded type.
Basically, it treats the case where iter_t is a reverse_iterator of the
iterators in my variant, and we need to check that we're applying visitation
on the proper iterator.
So line 43: is_same should be

On Fri, 2 Mar 2012, MM wrote:
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Jeremiah Willcock Sent: 01 March 2012 16:48 To: boost-users@lists.boost.org Subject: Re: [Boost-users] enable_if and templated operator() of member of class template
On Thu, 1 Mar 2012, MM wrote: 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
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
Thank you, http://codepad.org/916Tl6W8 compiled. Using std::enable_if as well. (msvc10 uses their TR1 impl).
I need an extra check for the 2nd operator() of the visitor, that variant_iter_t is actually identical to iter_t::Iterator embedded type. Basically, it treats the case where iter_t is a reverse_iterator of the iterators in my variant, and we need to check that we're applying visitation on the proper iterator. So line 43: is_same should be
the problem is that this operator is instantiated even for the case where iter_t is a const_iterator and not a const_reverse_iterator and compilation fails, because const_iterator has no embedded type Iterator.
How to fix?
Try this version instead: http://codepad.org/uA7oflvk. It uses a helper template to do the dispatching among the overloads. -- Jeremiah Willcock
participants (3)
-
Jeremiah Willcock
-
MM
-
Philipp Moeller