diff --git a/enable_if.html b/enable_if.html index a17a290..8ea33bb 100644 --- a/enable_if.html +++ b/enable_if.html @@ -21,6 +21,7 @@

Copyright 2003 Jaakko Järvi, Jeremiah Willcock, Andrew Lumsdaine.
+Copyright 2011 Matt Calabrese.

@@ -81,7 +82,7 @@ definitions to find this out. Instantiating the latter definition with
int::result_type negate(const int&);
 
 
-where the return type is invalid. If this was an error, adding an unrelated function template +where the return type is invalid. If this were an error, adding an unrelated function template (that was never called) could break otherwise valid code. Due to the SFINAE principle the above example is not, however, erroneous. The latter definition of negate is simply removed from the overload resolution set.
@@ -154,6 +155,7 @@ typename enable_if<boost::is_arithmetic<T>, T>::type foo(T t) { return t; } +

3  Using enable_if

@@ -162,8 +164,19 @@ foo(T t) { return t; } The enable_if templates are defined in boost/utility/enable_if.hpp, which is included by boost/utility.hpp.

-The enable_if template can be used either as the return type, or as an -extra argument. For example, the foo function in the previous section could also be written +With respect to function templates, enable_if can be used in multiple different ways: + + + +In the previous section, the return type form of enable_if was shown. As an example +of using the form of enable_if that works via an extra function parameter, the +foo function in the previous section could also be written as:
template <class T>
 T foo(T t, typename enable_if<boost::is_arithmetic<T> >::type* dummy = 0); 
@@ -173,18 +186,80 @@ a default value to keep the parameter hidden from client code.
 Note that the second template argument was not given to enable_if, as the default 
 void gives the desired behavior.

-Whether to write the enabler as an argument or within the return type is -largely a matter of taste, but for certain functions, only one -alternative is possible: +Which way to write the enabler is largely a matter of taste, but for certain functions, only a +subset of the options is possible: + + + +

3.1  Enabling function templates in C++0x

+ +In a compiler which supports C++0x default arguments for function template parameters, you can +enable and disable function templates by adding an additional template parameter. This approach +works in all situations where you would use either the return type form of enable_if or +the function parameter form, including operators, constructors, variadic function templates, and +even overloaded conversion operations. + +As an example: + +
#include <boost/type_traits/is_arithmetic.hpp>
+#include <boost/type_traits/is_pointer.hpp>
+#include <boost/utility/enable_if.hpp>
+
+class test
+{
+public:
+  // A constructor that works for any argument list of size 10
+  template< class... T
+          , typename boost::enable_if_c< sizeof...( T ) == 10, int >::type = 0
+          >
+  test( T&&... );
+
+  // A conversion operation that can convert to any arithmetic type
+  template< class T
+          , typename boost::enable_if< boost::is_arithmetic< T >, int >::type = 0
+          >
+  operator T() const;
+
+  // A conversion operation that can convert to any pointer type
+  template< class T
+          , typename boost::enable_if< boost::is_pointer< T >, int >::type = 0
+          >
+  operator T() const;
+};
+
+int main()
+{
+  // Works
+  test test_( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
+
+  // Fails as expected
+  test fail_construction( 1, 2, 3, 4, 5 );
+
+  // Works by calling the conversion operator enabled for arithmetic types
+  int arithmetic_object = test_;
+
+  // Works by calling the conversion operator enabled for pointer types
+  int* pointer_object = test_;
+
+  // Fails as expected
+  struct {} fail_conversion = test_;
+}
+
+
+ -

3.1  Enabling template class specializations

+

3.2  Enabling template class specializations

Class template specializations can be enabled or disabled with enable_if. @@ -210,7 +285,7 @@ is the correct value.

-

3.2  Overlapping enabler conditions

+

3.3  Overlapping enabler conditions

Once the compiler has examined the enabling conditions and included the @@ -239,7 +314,7 @@ partial specializations as well.

-

3.3  Lazy enable_if

+

3.4  Lazy enable_if

In some cases it is necessary to avoid instantiating part of a @@ -285,7 +360,7 @@ above example, is_multipliable<T, U>::value defines when
-

3.4  Compiler workarounds

+

3.5  Compiler workarounds

Some compilers flag functions as ambiguous if the only distinguishing factor is a different @@ -367,9 +442,9 @@ David Vandevoorde and Nicolai M. Josuttis. Addison-Wesley, 2002.
-

Copyright Jaakko Järvi, Jeremiah Willcock and Andrew Lumsdaine
-{jajarvi|jewillco|lums}@osl.iu.edu
-Indiana University
+

Copyright Jaakko Järvi*, Jeremiah Willcock*, Andrew Lumsdaine*, Matt Calabrese
+{jajarvi|jewillco|lums}@osl.iu.edu, rivorus@gmail.com
+*Indiana University
Open Systems Lab
Use, modification and distribution are subject to the Boost Software License, Version 1.0. @@ -386,4 +461,4 @@ or copy at HEVEA. - \ No newline at end of file +