Hi, I understand that by using enable_if the following way: template <class T> T foo(T t, typename enable_if<boost::is_arithmetic<T> >::type* dummy = 0); I can include/exclude the appropriate function overloads. When I write: template<class T> T foo(T t, typename enable_if<[condition1]>::type* dummy = 0, typename enable_if<[condition2]>::type* dummy = 0); Does this mean an AND-relationship between condition1 and condition2? (So that a particular overload is enabled if and only if both conditions evaluate to true?) Thx, Gus
On Jun 12, 2004, at 5:20 PM, Agoston Bejo wrote:
Hi, I understand that by using enable_if the following way:
template <class T> T foo(T t, typename enable_if<boost::is_arithmetic<T> >::type* dummy = 0);
I can include/exclude the appropriate function overloads. When I write:
template<class T> T foo(T t, typename enable_if<[condition1]>::type* dummy = 0, typename enable_if<[condition2]>::type* dummy = 0);
Does this mean an AND-relationship between condition1 and condition2? (So that a particular overload is enabled if and only if both conditions evaluate to true?)
Yes, it does mean "and". But it is a cryptic way to do it. Instead consider: template<class T> T foo(T t, typename enable_if<condition1 && condition2>::type* dummy = 0); Also, no need for the dummy name: template<class T> T foo(T t, typename enable_if<condition1 && condition2>::type* = 0); And you might prefer to place the condition on the return type for readability purposes: template <class T> typename enable_if < condition1 && condition2, T
::type foo(T t);
-Howard
Howard Hinnant <hinnant@twcny.rr.com> writes:
And you might prefer to place the condition on the return type for readability purposes:
template <class T> typename enable_if < condition1 && condition2, T
::type foo(T t);
Ahem. That would be: template <class T> typename enable_if < mpl::and_<condition1, condition2>, ^^^^^^^^^^ T
::type foo(T t);
or, if you insist, the usually-less-efficient template <class T> typename enable_if_c ^^ < condition1::value && condition2::value, T
::type foo(T t);
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
participants (3)
-
Agoston Bejo
-
David Abrahams
-
Howard Hinnant