mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
fix broken link to logo
[SVN r21122]
This commit is contained in:
parent
8024c3e9c7
commit
61fb5a0b8f
@ -4,7 +4,7 @@
|
|||||||
<HEAD><TITLE>enable_if</TITLE>
|
<HEAD><TITLE>enable_if</TITLE>
|
||||||
|
|
||||||
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
<META name="GENERATOR" content="hevea 1.06">
|
<META name="GENERATOR" content="Microsoft FrontPage 5.0">
|
||||||
</HEAD>
|
</HEAD>
|
||||||
<BODY >
|
<BODY >
|
||||||
<!--HEVEA command line is: hevea -nosymb -noiso -pedantic -v enable_if_docs_for_boost.tex -->
|
<!--HEVEA command line is: hevea -nosymb -noiso -pedantic -v enable_if_docs_for_boost.tex -->
|
||||||
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
|
|
||||||
<h1>
|
<h1>
|
||||||
<img border="0" src="c++boost.gif" align="center" width="277" height="86">enable_if</h1>
|
<img border="0" src="../../c++boost.gif" align="center" width="277" height="86">enable_if</h1>
|
||||||
<BR>
|
<BR>
|
||||||
<BR>
|
<BR>
|
||||||
Copyright 2003 Jaakko Järvi, Jeremiah Willcock, Andrew Lumsdaine.<BR>
|
Copyright 2003 Jaakko Järvi, Jeremiah Willcock, Andrew Lumsdaine.<BR>
|
||||||
@ -42,8 +42,7 @@ in [<A HREF="#jarvi:03:cuj_arbitrary_overloading"><CITE>1</CITE></A>] and&n
|
|||||||
<H3><A NAME="htoc2">1.1</A> Synopsis</H3><!--SEC END -->
|
<H3><A NAME="htoc2">1.1</A> Synopsis</H3><!--SEC END -->
|
||||||
|
|
||||||
<A NAME="sec:synopsis"></A>
|
<A NAME="sec:synopsis"></A>
|
||||||
<PRE>
|
<PRE>namespace boost {
|
||||||
namespace boost {
|
|
||||||
template <class Cond, class T = void> struct enable_if;
|
template <class Cond, class T = void> struct enable_if;
|
||||||
template <class Cond, class T = void> struct disable_if;
|
template <class Cond, class T = void> struct disable_if;
|
||||||
template <class Cond, class T> struct lazy_enable_if;
|
template <class Cond, class T> struct lazy_enable_if;
|
||||||
@ -68,9 +67,7 @@ template, the instantiation is removed from the overload resolution
|
|||||||
set instead of causing a compilation error. The following example,
|
set instead of causing a compilation error. The following example,
|
||||||
taken from [<A HREF="#jarvi:03:cuj_arbitrary_overloading"><CITE>1</CITE></A>],
|
taken from [<A HREF="#jarvi:03:cuj_arbitrary_overloading"><CITE>1</CITE></A>],
|
||||||
demonstrates why this is important:
|
demonstrates why this is important:
|
||||||
<PRE>
|
<PRE>int negate(int i) { return -i; }
|
||||||
|
|
||||||
int negate(int i) { return -i; }
|
|
||||||
|
|
||||||
template <class F>
|
template <class F>
|
||||||
typename F::result_type negate(const F& f) { return -f(); }
|
typename F::result_type negate(const F& f) { return -f(); }
|
||||||
@ -81,9 +78,7 @@ definition is obviously a better match, but the compiler must
|
|||||||
nevertheless consider (and instantiate the prototypes) of both
|
nevertheless consider (and instantiate the prototypes) of both
|
||||||
definitions to find this out. Instantiating the latter definition with
|
definitions to find this out. Instantiating the latter definition with
|
||||||
<TT>F</TT> as <TT>int</TT> would result in:
|
<TT>F</TT> as <TT>int</TT> would result in:
|
||||||
<PRE>
|
<PRE>int::result_type negate(const int&);
|
||||||
|
|
||||||
int::result_type negate(const int&);
|
|
||||||
|
|
||||||
</PRE>
|
</PRE>
|
||||||
where the return type is invalid. If this was an error, adding an unrelated function template
|
where the return type is invalid. If this was an error, adding an unrelated function template
|
||||||
@ -111,9 +106,7 @@ The latter version interoperates with Boost.MPL. <BR>
|
|||||||
<BR>
|
<BR>
|
||||||
The definitions of <TT>enable_if_c</TT> and <TT>enable_if</TT> are as follows (we use <TT>enable_if</TT> templates
|
The definitions of <TT>enable_if_c</TT> and <TT>enable_if</TT> are as follows (we use <TT>enable_if</TT> templates
|
||||||
unqualified but they are in the <TT>boost</TT> namespace).
|
unqualified but they are in the <TT>boost</TT> namespace).
|
||||||
<PRE>
|
<PRE>template <bool B, class T = void>
|
||||||
|
|
||||||
template <bool B, class T = void>
|
|
||||||
struct enable_if_c {
|
struct enable_if_c {
|
||||||
typedef T type;
|
typedef T type;
|
||||||
};
|
};
|
||||||
@ -136,9 +129,7 @@ The <TT>enable_if_c</TT> template can thus be used for controlling when function
|
|||||||
overload resolution and when they are not.
|
overload resolution and when they are not.
|
||||||
For example, the following function is defined for all arithmetic types (according to the
|
For example, the following function is defined for all arithmetic types (according to the
|
||||||
classification of the <A HREF="http://www.boost.org/libs/type_traits">Boost type_traits library</A>):
|
classification of the <A HREF="http://www.boost.org/libs/type_traits">Boost type_traits library</A>):
|
||||||
<PRE>
|
<PRE>template <class T>
|
||||||
|
|
||||||
template <class T>
|
|
||||||
typename enable_if_c<boost::is_arithmetic<T>::value, T>::type
|
typename enable_if_c<boost::is_arithmetic<T>::value, T>::type
|
||||||
foo(T t) { return t; }
|
foo(T t) { return t; }
|
||||||
|
|
||||||
@ -146,9 +137,7 @@ foo(T t) { return t; }
|
|||||||
The <TT>disable_if_c</TT> template is provided as well, and has the
|
The <TT>disable_if_c</TT> template is provided as well, and has the
|
||||||
same functionality as <TT>enable_if_c</TT> except for the negated condition. The following
|
same functionality as <TT>enable_if_c</TT> except for the negated condition. The following
|
||||||
function is enabled for all non-arithmetic types.
|
function is enabled for all non-arithmetic types.
|
||||||
<PRE>
|
<PRE>template <class T>
|
||||||
|
|
||||||
template <class T>
|
|
||||||
typename disable_if_c<boost::is_arithmetic<T>::value, T>::type
|
typename disable_if_c<boost::is_arithmetic<T>::value, T>::type
|
||||||
bar(T t) { return t; }
|
bar(T t) { return t; }
|
||||||
|
|
||||||
@ -160,9 +149,7 @@ The MPL <TT>bool_</TT>, <TT>and_</TT>, <TT>or_</TT>, and <TT>not_</TT> templates
|
|||||||
useful for creating such types. Also, the traits classes in the Boost.Type_traits library
|
useful for creating such types. Also, the traits classes in the Boost.Type_traits library
|
||||||
follow this convention.
|
follow this convention.
|
||||||
For example, the above example function <TT>foo</TT> can be alternatively written as:
|
For example, the above example function <TT>foo</TT> can be alternatively written as:
|
||||||
<PRE>
|
<PRE>template <class T>
|
||||||
|
|
||||||
template <class T>
|
|
||||||
typename enable_if<boost::is_arithmetic<T>, T>::type
|
typename enable_if<boost::is_arithmetic<T>, T>::type
|
||||||
foo(T t) { return t; }
|
foo(T t) { return t; }
|
||||||
|
|
||||||
@ -178,9 +165,7 @@ The <TT>enable_if</TT> templates are defined in
|
|||||||
The <TT>enable_if</TT> template can be used either as the return type, or as an
|
The <TT>enable_if</TT> template can be used either as the return type, or as an
|
||||||
extra argument. For example, the <TT>foo</TT> function in the previous section could also be written
|
extra argument. For example, the <TT>foo</TT> function in the previous section could also be written
|
||||||
as:
|
as:
|
||||||
<PRE>
|
<PRE>template <class T>
|
||||||
|
|
||||||
template <class T>
|
|
||||||
T foo(T t, typename enable_if<boost::is_arithmetic<T> >::type* dummy = 0);
|
T foo(T t, typename enable_if<boost::is_arithmetic<T> >::type* dummy = 0);
|
||||||
|
|
||||||
</PRE>Hence, an extra parameter of type <TT>void*</TT> is added, but it is given
|
</PRE>Hence, an extra parameter of type <TT>void*</TT> is added, but it is given
|
||||||
@ -206,9 +191,7 @@ Class template specializations can be enabled or disabled with <TT>enable_if</TT
|
|||||||
One extra template parameter needs to be added for the enabler expressions.
|
One extra template parameter needs to be added for the enabler expressions.
|
||||||
This parameter has the default value <TT>void</TT>.
|
This parameter has the default value <TT>void</TT>.
|
||||||
For example:
|
For example:
|
||||||
<PRE>
|
<PRE>template <class T, class Enable = void>
|
||||||
|
|
||||||
template <class T, class Enable = void>
|
|
||||||
class A { ... };
|
class A { ... };
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
@ -236,9 +219,7 @@ rules are used to select the best matching function.
|
|||||||
In particular, there is no ordering between enabling conditions.
|
In particular, there is no ordering between enabling conditions.
|
||||||
Function templates with enabling conditions that are not mutually exclusive can
|
Function templates with enabling conditions that are not mutually exclusive can
|
||||||
lead to ambiguities. For example:
|
lead to ambiguities. For example:
|
||||||
<PRE>
|
<PRE>template <class T>
|
||||||
|
|
||||||
template <class T>
|
|
||||||
typename enable_if<boost::is_integral<T>, void>::type
|
typename enable_if<boost::is_integral<T>, void>::type
|
||||||
foo(T t) {}
|
foo(T t) {}
|
||||||
|
|
||||||
@ -263,9 +244,7 @@ partial specializations as well.<BR>
|
|||||||
<A NAME="sec:enable_if_lazy"></A>
|
<A NAME="sec:enable_if_lazy"></A>
|
||||||
In some cases it is necessary to avoid instantiating part of a
|
In some cases it is necessary to avoid instantiating part of a
|
||||||
function signature unless an enabling condition is true. For example:
|
function signature unless an enabling condition is true. For example:
|
||||||
<PRE>
|
<PRE>template <class T, class U> class mult_traits;
|
||||||
|
|
||||||
template <class T, class U> class mult_traits;
|
|
||||||
|
|
||||||
template <class T, class U>
|
template <class T, class U>
|
||||||
typename enable_if<is_multipliable<T, U>, typename mult_traits<T, U>::type>::type
|
typename enable_if<is_multipliable<T, U>, typename mult_traits<T, U>::type>::type
|
||||||
@ -284,9 +263,7 @@ The SFINAE principle is not applied because
|
|||||||
the invalid type occurs as an argument to another template. The <TT>lazy_enable_if</TT>
|
the invalid type occurs as an argument to another template. The <TT>lazy_enable_if</TT>
|
||||||
and <TT>lazy_disable_if</TT> templates (and their <TT>_c</TT> versions) can be used in such
|
and <TT>lazy_disable_if</TT> templates (and their <TT>_c</TT> versions) can be used in such
|
||||||
situations:
|
situations:
|
||||||
<PRE>
|
<PRE>template<class T, class U>
|
||||||
|
|
||||||
template<class T, class U>
|
|
||||||
typename lazy_enable_if<is_multipliable<T, U>, mult_traits<T, U> >::type
|
typename lazy_enable_if<is_multipliable<T, U>, mult_traits<T, U> >::type
|
||||||
operator*(const T& t, const U& u) { ... }
|
operator*(const T& t, const U& u) { ... }
|
||||||
|
|
||||||
@ -314,9 +291,7 @@ above example, <TT>is_multipliable<T, U>::value</TT> defines when
|
|||||||
Some compilers flag functions as ambiguous if the only distinguishing factor is a different
|
Some compilers flag functions as ambiguous if the only distinguishing factor is a different
|
||||||
condition in an enabler (even though the functions could never be ambiguous). For example,
|
condition in an enabler (even though the functions could never be ambiguous). For example,
|
||||||
some compilers (e.g. GCC 3.2) diagnose the following two functions as ambiguous:
|
some compilers (e.g. GCC 3.2) diagnose the following two functions as ambiguous:
|
||||||
<PRE>
|
<PRE>template <class T>
|
||||||
|
|
||||||
template <class T>
|
|
||||||
typename enable_if<boost::is_arithmetic<T>, T>::type
|
typename enable_if<boost::is_arithmetic<T>, T>::type
|
||||||
foo(T t);
|
foo(T t);
|
||||||
|
|
||||||
@ -328,9 +303,7 @@ foo(T t);
|
|||||||
<UL><LI>
|
<UL><LI>
|
||||||
Use an extra dummy parameter which disambiguates the functions. Use a default value for
|
Use an extra dummy parameter which disambiguates the functions. Use a default value for
|
||||||
it to hide the parameter from the caller. For example:
|
it to hide the parameter from the caller. For example:
|
||||||
<PRE>
|
<PRE>template <class T> struct dummy { dummy(int) {} };
|
||||||
|
|
||||||
template <class T> struct dummy { dummy(int) {} };
|
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
typename enable_if<boost::is_arithmetic<T>, T>::type
|
typename enable_if<boost::is_arithmetic<T>, T>::type
|
||||||
@ -343,9 +316,7 @@ foo(T t, dummy<1> = 0);
|
|||||||
<BR>
|
<BR>
|
||||||
<LI>Define the functions in different namespaces and bring them into a common
|
<LI>Define the functions in different namespaces and bring them into a common
|
||||||
namespace with <TT>using</TT> declarations:
|
namespace with <TT>using</TT> declarations:
|
||||||
<PRE>
|
<PRE>namespace A {
|
||||||
|
|
||||||
namespace A {
|
|
||||||
template <class T>
|
template <class T>
|
||||||
typename enable_if<boost::is_arithmetic<T>, T>::type
|
typename enable_if<boost::is_arithmetic<T>, T>::type
|
||||||
foo(T t);
|
foo(T t);
|
||||||
@ -414,4 +385,4 @@ Open Systems Lab
|
|||||||
</EM><A HREF="http://pauillac.inria.fr/~maranget/hevea/index.html"><EM>H<FONT SIZE=2><sup>E</sup></FONT>V<FONT SIZE=2><sup>E</sup></FONT>A</EM></A><EM>.
|
</EM><A HREF="http://pauillac.inria.fr/~maranget/hevea/index.html"><EM>H<FONT SIZE=2><sup>E</sup></FONT>V<FONT SIZE=2><sup>E</sup></FONT>A</EM></A><EM>.
|
||||||
</EM></BLOCKQUOTE>
|
</EM></BLOCKQUOTE>
|
||||||
</BODY>
|
</BODY>
|
||||||
</HTML>
|
</HTML>
|
Loading…
x
Reference in New Issue
Block a user