diff --git a/overloaded_function/doc/Jamfile.v2 b/overloaded_function/doc/Jamfile.v2 new file mode 100644 index 0000000..f09dabf --- /dev/null +++ b/overloaded_function/doc/Jamfile.v2 @@ -0,0 +1,30 @@ + +# Copyright (C) 2009-2012 Lorenzo Caminiti +# Distributed under the Boost Software License, Version 1.0 +# (see accompanying file LICENSE_1_0.txt or a copy at +# http://www.boost.org/LICENSE_1_0.txt) +# Home at http://www.boost.org/libs/functional/overloaded_function + +import quickbook ; +using boostbook ; + +doxygen reference + : ../../../../boost/functional/overloaded_function.hpp + ../../../../boost/functional/overloaded_function/config.hpp + : "Reference" + PREDEFINED="DOXYGEN" + QUIET=YES + WARN_IF_UNDOCUMENTED=NO + HIDE_UNDOC_MEMBERS=YES + HIDE_UNDOC_CLASSES=YES + ALIASES=" Params=\"Parameters: \" Param{2}=\"\" EndParams=\"
\\1\\2
\" Returns=\"Returns:\" Note=\"Note:\" Warning=\"Warning:\" See=\"See:\" RefSect{1}=\"\\xmlonly\\1\\endxmlonly\" RefSectId{2}=\"\\xmlonly\\2\\endxmlonly\" RefClass{1}=\"\\xmlonly\\1\\endxmlonly\" RefFunc{1}=\"\\xmlonly\\1\\endxmlonly\" RefMacro{1}=\"\\xmlonly\\1\\endxmlonly\" " + ; + +xml qbk : overloaded_function.qbk : reference ; + +boostbook doc : qbk + : boost.root=../../../../.. + boost.defaults=Boost + pdf:boost.url.prefix=http://www.boost.org/doc/libs/release/libs/functional/overloaded_function/doc/html + ; + diff --git a/overloaded_function/doc/html/BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX.html b/overloaded_function/doc/html/BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX.html new file mode 100644 index 0000000..209a321 --- /dev/null +++ b/overloaded_function/doc/html/BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX.html @@ -0,0 +1,54 @@ + + + +Macro BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+
+
+

Macro BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX

+

BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX — Specify the maximum number of arguments of the functions being overloaded.

+
+

Synopsis

+
// In header: <boost/functional/overloaded_function/config.hpp>
+
+BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX
+
+

Description

+

If this macro is left undefined by the user, it has a default value of 5 (increasing this number might increase compilation time). When specified by the user, this macro must be a non-negative integer number.

+

See: Getting Started, boost::overloaded_function.

+
+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/overloaded_function/doc/html/BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX.html b/overloaded_function/doc/html/BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX.html new file mode 100644 index 0000000..391cc27 --- /dev/null +++ b/overloaded_function/doc/html/BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX.html @@ -0,0 +1,54 @@ + + + +Macro BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+
+
+

Macro BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX

+

BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX — Specify the maximum number of functions that can be overloaded.

+
+

Synopsis

+
// In header: <boost/functional/overloaded_function/config.hpp>
+
+BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX
+
+

Description

+

If this macro is left undefined by the user, it has a default value of 5 (increasing this number might increase compilation time). When defined by the user, this macro must be an integer number greater or equal than 2 (because at least two distinct functions need to be specified in order to define an overload).

+

See: Getting Started, boost::overloaded_function.

+
+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/overloaded_function/doc/html/boost/make_overloaded_function.html b/overloaded_function/doc/html/boost/make_overloaded_function.html new file mode 100644 index 0000000..eda4191 --- /dev/null +++ b/overloaded_function/doc/html/boost/make_overloaded_function.html @@ -0,0 +1,60 @@ + + + +Function template make_overloaded_function + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+
+
+

Function template make_overloaded_function

+

boost::make_overloaded_function — Make an overloaded function object without explicitly specifying the function types.

+
+

Synopsis

+
// In header: <boost/functional/overloaded_function.hpp>
+
+
+template<typename F1, typename F2, ... > 
+  overloaded_function< __function_type__< F1 >, __function_type__< F2 >,...> 
+  make_overloaded_function(F1 f1, F2 f2, ...);
+
+

Description

+

This function template creates and returns an overloaded_function function object that overloads all the specified functions f1, f2, etc.

+

The function types are internally determined from the template parameter types so they do not need to be explicitly specified. Therefore, this function template usually has a more concise syntax when compared with overloaded_function. This is especially useful when the explicit type of the returned overloaded_function does not need to be known (e.g., when used with Boost.Typeof's BOOST_AUTO (or C++11 auto) or when the overloaded function object is handled using a function template parameter, see the Tutorial section).

+

The maximum number of functions to overload is given by the BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX configuration macro.

+

Note: In this documentation, __function_type__ is a placeholder for a symbol that is specific to implementation of this library.

+

See: Tutorial section, overloaded_function, BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX.

+
+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/overloaded_function/doc/html/boost/overloaded_function.html b/overloaded_function/doc/html/boost/overloaded_function.html new file mode 100644 index 0000000..6257929 --- /dev/null +++ b/overloaded_function/doc/html/boost/overloaded_function.html @@ -0,0 +1,99 @@ + + + +Class template overloaded_function + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+
+
+

Class template overloaded_function

+

boost::overloaded_function — Function object to overload functions with distinct signatures.

+
+

Synopsis

+
// In header: <boost/functional/overloaded_function.hpp>
+
+template<typename F1, typename F2, ... > 
+class overloaded_function {
+public:
+  // construct/copy/destruct
+  overloaded_function(const boost::function< F1 > &, 
+                      const boost::function< F2 > &, ...);
+
+  // public member functions
+  boost::function_traits< F1 >::result_type 
+  operator()(typename boost::function_traits< F1 >::arg1_type, 
+             typename boost::function_traits< F1 >::arg2_type, ...) const;
+  boost::function_traits< F2 >::result_type 
+  operator()(typename boost::function_traits< F2 >::arg1_type, 
+             typename boost::function_traits< F2 >::arg2_type, ...) const;
+};
+
+

Description

+

This function object aggregates together calls to functions of all the specified function types F1, F2, etc. The specified function types must have distinct parameters from one another and they must be in the following format (which is the Boost.Function preferred syntax):

+
    result_type (argument1_type, argumgnet2_type, ...)
+
+

In some cases, the make_overloaded_function function template can be useful to construct the overloaded function object without explicitly specifying the function types.

+

The maximum number of functions to overload is given by the BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX configuration macro. The maximum number of function parameters for each of the specified function types is given by the BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX configuration macro.

+

See: Tutorial section, make_overloaded_function, BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX, BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX, Boost.Function.

+
+

+overloaded_function + public + construct/copy/destruct

+
  1. +
    overloaded_function(const boost::function< F1 > &, 
    +                    const boost::function< F2 > &, ...);
    Construct the overloaded function object.

    Any function pointer, function reference, and monomorphic function object that can be converted to a boost::function function object can be specified as parameter.

    +
+
+
+

+overloaded_function public member functions

+
    +
  1. +
    boost::function_traits< F1 >::result_type 
    +operator()(typename boost::function_traits< F1 >::arg1_type, 
    +           typename boost::function_traits< F1 >::arg2_type, ...) const;
    Call operator matching the signature of the function type specified as 1st template parameter.

    This will in turn invoke the call operator of the 1st function passed to the constructor.

    +
  2. +
  3. +
    boost::function_traits< F2 >::result_type 
    +operator()(typename boost::function_traits< F2 >::arg1_type, 
    +           typename boost::function_traits< F2 >::arg2_type, ...) const;
    Call operator matching the signature of the function type specified as 2nd template parameter.

    This will in turn invoke the call operator of the 2nd function passed to the constructor.

    +

    Note: Similar call operators are present for all specified function types F1, F2, etc (even if not exhaustively listed by this documentation).

    +
  4. +
+
+
+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/overloaded_function/doc/html/boost_functional_overloadedfunction/Acknowledgments.html b/overloaded_function/doc/html/boost_functional_overloadedfunction/Acknowledgments.html new file mode 100644 index 0000000..f03dd92 --- /dev/null +++ b/overloaded_function/doc/html/boost_functional_overloadedfunction/Acknowledgments.html @@ -0,0 +1,60 @@ + + + +Acknowledgments + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHome +
+
+ +

+ Many thanks to Mathias Gaunard for suggesting to implement boost::overloaded_function + and for some sample code. +

+

+ Thanks to John Bytheway for suggesting to implement boost::make_overloaded_function. +

+

+ Thanks to Nathan Ridge for suggestions on how to implement boost::make_overloaded_function. +

+

+ Thanks to Robert Stewart for commenting on the library name. +

+

+ Many thanks to the entire Boost community + and mailing list for providing valuable comments about this library and great + insights on the C++ programming language. +

+
+ + + +
+
+
+PrevUpHome +
+ + diff --git a/overloaded_function/doc/html/boost_functional_overloadedfunction/Getting_Started.html b/overloaded_function/doc/html/boost_functional_overloadedfunction/Getting_Started.html new file mode 100644 index 0000000..0a15517 --- /dev/null +++ b/overloaded_function/doc/html/boost_functional_overloadedfunction/Getting_Started.html @@ -0,0 +1,81 @@ + + + +Getting Started + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ + +

+ This section explains how to setup a system to use this library. +

+
+ +

+ The library was tested by the authors on GCC 4.5.3 (with and without C++11 + features -std=c++0x) and MSVC 8.0 under Linux, Cygwin, + and Windows 7. +

+
+
+ +

+ This library is composed of header files only. Therefore there is no pre-compiled + object file which needs to be installed. Programmers can simply instruct + the compiler where to find the library header files (-I option on GCC, /I option on MSVC, etc) and compile code + using the library. +

+

+ The maximum number of functions to overload is given by the BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX + configuration macro. The maximum number of function parameters for each of + the specified function types is given by the BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX + configuration macro. All configuration macros have appropriate default values + when they are left undefined by the user. +

+
+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/overloaded_function/doc/html/boost_functional_overloadedfunction/Tutorial.html b/overloaded_function/doc/html/boost_functional_overloadedfunction/Tutorial.html new file mode 100644 index 0000000..ffd899d --- /dev/null +++ b/overloaded_function/doc/html/boost_functional_overloadedfunction/Tutorial.html @@ -0,0 +1,202 @@ + + + +Tutorial + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ + +

+ This section explains how to use this library. +

+
+ +

+ Consider the following functions with distinct signatures: +

+

+

+
const std::string& identity_s(const std::string& x) { return x; } // As pointer.
+
+int identity_i_impl(int x) { return x; }
+int (&identity_i)(int) = identity_i_impl; // Function reference.
+
+double identity_d_impl(double x) { return x; }
+boost::function<double (double)> identity_d = identity_d_impl; // Functor.
+
+

+

+

+ This library header boost/functional/overloaded_function.hpp + provides a boost::overloaded_function + class template that creates a single overloaded function object that can + be used to call the specified functions instead of using the separate function + names (see also identity.cpp): +

+

+

+
boost::overloaded_function<
+      const std::string& (const std::string&)
+    , int (int)
+    , double (double)
+> identity(identity_s, identity_i, identity_d);
+
+// All calls via single `identity` function.
+BOOST_CHECK( identity("abc") == "abc" );
+BOOST_CHECK( identity(123) == 123 );
+BOOST_CHECK( identity(1.23) == 1.23 );
+
+

+

+

+ Note how function types in the following format are passed as template parameters + to boost::overloaded_function + (this is the format of Boost.Function's + preferred syntax): +

+
result-type (argument1-type, argument2-type, ...)
+
+

+ Then the relative function pointers, function references, or monomorphic + function objects are passed to the boost::overloaded_function + constructor matching the order of the specified template parameters. [2] In the above example, identity_s + is passed as function pointer (the function address is automatically taken + from the function name by compiler), identity_i + as function reference, and identity_d + as function object. +

+

+ All specified function types must have distinct parameters from one another + (so the overloaded calls can be resolved by this library). [3] In order to create an overloaded function object, it is necessary + to specify at least two function types (because there is nothing to overload + between one or zero functions). +

+
+
+ +

+ For convenience, this library also provides the boost::make_overloaded_function + function template which allows to create the overloaded function object without + explicitly specifying the function types. The function types are automatically + deduced from the specified functions and the appropriate boost::overloaded_function + instantiation is returned by boost::make_overloaded_function. +

+

+ The boost::make_overloaded_function + function template can be useful when used together with Boost.Typeof's + BOOST_AUTO (or C++11 auto). For example (see also identity.cpp): +

+

+

+
BOOST_AUTO(identity, boost::make_overloaded_function(
+        identity_s, identity_i, identity_d));
+
+BOOST_CHECK( identity("abc") == "abc" );
+BOOST_CHECK( identity(123) == 123 );
+BOOST_CHECK( identity(1.23) == 1.23 );
+
+

+

+

+ Note how the overloaded function object identity + has been created specifying only the functions identity_s, + identity_i, identity_d and without specifying the function + types const std::string& (const + std::string&), + int (int), and + double (double) as + required instead by boost::overloaded_function. + Therefore, boost::make_overloaded_function + provides a more concise syntax in this context when compared with boost::overloaded_function. +

+

+ Another case where boost::make_overloaded_function + can be useful is when the overloaded function object is passed to a function + template which can hide the specific boost::overloaded_function + type using a template parameter. For example (see also identity.cpp): +

+

+

+
template<typename F>
+void check(F identity) {
+    BOOST_CHECK( identity("abc") == "abc" );
+    BOOST_CHECK( identity(123) == 123 );
+    BOOST_CHECK( identity(1.23) == 1.23 );
+}
+
+

+

+

+

+
check(boost::make_overloaded_function(identity_s, identity_i, identity_d));
+
+

+

+
+
+

+

[2] + Function pointers are of the form result-type (*)(argument1-type, ...) (the + C++ compiler is usually able to automatically promote a function name to + a function pointer in a context where a function pointer is expected even + if the function name is not prefixed by &). + Function references are of the form result-type (&)(argument1-type, ...). + Function types are of the form result-type (argument1-type, ...) (note + how they lack of both * and + & when compared to function + pointers and function references). Finally, monomorphic function objects + are instances of classes with a non-template call operator of the form + result-type operator()(argument1-type, ...). +

+

[3] + Note that in C++ the function result type is not used for overload resolution + (to avoid making the overload resolution context dependent). Therefore, + at least one of the function parameters must be distinct for each specified + function type. +

+
+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/overloaded_function/doc/html/index.html b/overloaded_function/doc/html/index.html new file mode 100644 index 0000000..c0946fd --- /dev/null +++ b/overloaded_function/doc/html/index.html @@ -0,0 +1,142 @@ + + + +Chapter 1. Boost.Functional/OverloadedFunction 1.0.0 + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
Next
+
+
+

+Chapter 1. Boost.Functional/OverloadedFunction 1.0.0

+

+Lorenzo Caminiti +

+
+
+

+ Distributed under the Boost Software License, Version 1.0 (see accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +

+
+
+ +

+ This library allows to overload different functions into a single function object. +

+
+ +

+ Consider the following functions with distinct signatures: +

+

+

+
const std::string& identity_s(const std::string& x) { return x; } // As pointer.
+
+int identity_i_impl(int x) { return x; }
+int (&identity_i)(int) = identity_i_impl; // Function reference.
+
+double identity_d_impl(double x) { return x; }
+boost::function<double (double)> identity_d = identity_d_impl; // Functor.
+
+

+

+

+ Instead of calling them using their separate names (here BOOST_CHECK + is equivalent to assert): + [1] +

+

+

+
BOOST_CHECK( identity_s("abc") == "abc" );
+BOOST_CHECK( identity_i(123) == 123 );
+BOOST_CHECK( identity_d(1.23) == 1.23 );
+
+

+

+

+ It is possible to use this library to create a single overloaded + function object (or functor) + named identity that aggregates + together the calls to the specific functions (see also identity.cpp): +

+

+

+
boost::overloaded_function<
+      const std::string& (const std::string&)
+    , int (int)
+    , double (double)
+> identity(identity_s, identity_i, identity_d);
+
+// All calls via single `identity` function.
+BOOST_CHECK( identity("abc") == "abc" );
+BOOST_CHECK( identity(123) == 123 );
+BOOST_CHECK( identity(1.23) == 1.23 );
+
+

+

+

+ Note how the functions are called via a single overloaded function object + identity instead of using their + different names identity_s, + identity_i, and identity_d. +

+
+
+

+

[1] + In the examples presented in this documentation, BOOST_CHECK + is used instead of assert + because it allows to write regression tests using Boost.Test. + The examples of this documentation are executed as part of the library test + suite to verify that they always compile and run correctly. +

+
+
+ + + +

Last revised: February 14, 2012 at 16:16:51 GMT

+
+
Next
+ + diff --git a/overloaded_function/doc/html/reference.html b/overloaded_function/doc/html/reference.html new file mode 100644 index 0000000..4dd4a17 --- /dev/null +++ b/overloaded_function/doc/html/reference.html @@ -0,0 +1,66 @@ + + + +Reference + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+

+Reference

+ +
+ +

Overload distinct function pointers, function references, and function objects into a single function object.

+
namespace boost {
+  template<typename F1, typename F2, ... > class overloaded_function;
+  template<typename F1, typename F2, ... > 
+    overloaded_function< __function_type__< F1 >, __function_type__< F2 >,...> 
+    make_overloaded_function(F1, F2, ...);
+}
+
+ +
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/overloaded_function/doc/overloaded_function.qbk b/overloaded_function/doc/overloaded_function.qbk new file mode 100644 index 0000000..0c1d589 --- /dev/null +++ b/overloaded_function/doc/overloaded_function.qbk @@ -0,0 +1,154 @@ + +[/ Copyright (C) 2009-2012 Lorenzo Caminiti ] +[/ Distributed under the Boost Software License, Version 1.0 ] +[/ (see accompanying file LICENSE_1_0.txt or a copy at ] +[/ http://www.boost.org/LICENSE_1_0.txt) ] +[/ Home at http://www.boost.org/libs/functional/overloaded_function ] + +[library Boost.Functional/OverloadedFunction + [quickbook 1.5] + [version 1.0.0] + [copyright 2011-2012 Lorenzo Caminiti] + [purpose overload functions with one function object] + [license + Distributed under the Boost Software License, Version 1.0 + (see accompanying file LICENSE_1_0.txt or copy at + [@http://www.boost.org/LICENSE_1_0.txt]) + ] + [authors [Caminiti lorcaminiti@gmail.com, Lorenzo]] + [category Function Objects and Higher-Order Programming] +] + +[def __Introduction__ [link functional_overloaded_function.Introduction Introduction]] +[def __Getting_Started__ [link functional_overloaded_function.Getting_Started Getting Started]] +[def __Tutorial__ [link functional_overloaded_function.Tutorial Tutorial]] +[def __Boost__ [@http://www.boost.org Boost]] +[def __Boost_Test__ [@http://www.boost.org/libs/test Boost.Test]] +[def __Boost_Function__ [@http://www.boost.org/libs/function Boost.Function]] +[def __Boost_Typeof__ [@http://www.boost.org/doc/libs/typeof Boost.Typeof]] + +[import ../test/identity.cpp] + +This library allows to overload different functions into a single function object. + +[section:Introduction Introduction] + +Consider the following functions with distinct signatures: + +[identity_decls] + +Instead of calling them using their separate names (here `BOOST_CHECK` is equivalent to `assert`): +[footnote +In the examples presented in this documentation, `BOOST_CHECK` is used instead of `assert` because it allows to write regression tests using __Boost_Test__. +The examples of this documentation are executed as part of the library test suite to verify that they always compile and run correctly. +] + +[identity_calls] + +It is possible to use this library to create a single [@http://en.wikipedia.org/wiki/Function_overloading overloaded] function object (or [@http://en.wikipedia.org/wiki/Functor functor]) named `identity` that aggregates together the calls to the specific functions (see also [@../../test/identity.cpp =identity.cpp=]): + +[identity] + +Note how the functions are called via a single overloaded function object `identity` instead of using their different names `identity_s`, `identity_i`, and `identity_d`. + +[endsect] + +[section:Getting_Started Getting Started] + +This section explains how to setup a system to use this library. + +[section Compilers and Platforms] + +The library was tested by the authors on GCC 4.5.3 (with and without C++11 features [^-std=c++0x]) and MSVC 8.0 under Linux, Cygwin, and Windows 7. + +[endsect] + +[section Installation] + +This library is composed of header files only. +Therefore there is no pre-compiled object file which needs to be installed. +Programmers can simply instruct the compiler where to find the library header files (`-I` option on GCC, `/I` option on MSVC, etc) and compile code using the library. + +The maximum number of functions to overload is given by the [macroref BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_OVERLOAD_MAX] configuration macro. +The maximum number of function parameters for each of the specified function types is given by the [macroref BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX] configuration macro. +All configuration macros have appropriate default values when they are left undefined by the user. + +[endsect] + +[endsect] + +[section:Tutorial Tutorial] + +This section explains how to use this library. + +[section Overloading] + +Consider the following functions with distinct signatures: + +[identity_decls] + +This library header [headerref boost/functional/overloaded_function.hpp] provides a [classref boost::overloaded_function] class template that creates a single overloaded function object that can be used to call the specified functions instead of using the separate function names (see also [@../../test/identity.cpp =identity.cpp=]): + +[identity] + +Note how function types in the following format are passed as template parameters to [classref boost::overloaded_function] (this is the format of __Boost_Function__'s preferred syntax): + + ``/result-type/`` (``/argument1-type/``, ``/argument2-type/``, ...) + +Then the relative function pointers, function references, or [@http://en.wikipedia.org/wiki/Polymorphism_(computer_science) monomorphic function] objects are passed to the [classref boost::overloaded_function] constructor matching the order of the specified template parameters. +[footnote +Function pointers are of the form [^['result-type ]]`(*)(`[^['argument1-type]]`, ...)` (the C++ compiler is usually able to automatically promote a function name to a function pointer in a context where a function pointer is expected even if the function name is not prefixed by `&`). +Function references are of the form [^['result-type ]]`(&)(`[^['argument1-type]]`, ...)`. +Function types are of the form [^['result-type ]]`(`[^['argument1-type]]`, ...)` (note how they lack of both `*` and `&` when compared to function pointers and function references). +Finally, monomorphic function objects are instances of classes with a non-template call operator of the form [^['result-type ]]`operator()(`[^['argument1-type]]`, ...)`. +] +In the above example, `identity_s` is passed as function pointer (the function address is automatically taken from the function name by compiler), `identity_i` as function reference, and `identity_d` as function object. + +All specified function types must have distinct parameters from one another (so the overloaded calls can be resolved by this library). +[footnote +Note that in C++ the function result type is not used for overload resolution (to avoid making the overload resolution context dependent). +Therefore, at least one of the function parameters must be distinct for each specified function type. +] +In order to create an overloaded function object, it is necessary to specify at least two function types (because there is nothing to overload between one or zero functions). + +[endsect] + +[section Without Function Types] + +For convenience, this library also provides the [funcref boost::make_overloaded_function] function template which allows to create the overloaded function object without explicitly specifying the function types. +The function types are automatically deduced from the specified functions and the appropriate [classref boost::overloaded_function] instantiation is returned by [funcref boost::make_overloaded_function]. + +The [funcref boost::make_overloaded_function] function template can be useful when used together with __Boost_Typeof__'s `BOOST_AUTO` (or C++11 `auto`). +For example (see also [@../../test/identity.cpp =identity.cpp=]): + +[identity_make] + +Note how the overloaded function object `identity` has been created specifying only the functions `identity_s`, `identity_i`, `identity_d` and without specifying the function types `const std::string& (const std::string&)`, `int (int)`, and `double (double)` as required instead by [classref boost::overloaded_function]. +Therefore, [funcref boost::make_overloaded_function] provides a more concise syntax in this context when compared with [classref boost::overloaded_function]. + +Another case where [funcref boost::make_overloaded_function] can be useful is when the overloaded function object is passed to a function template which can hide the specific [classref boost::overloaded_function] type using a template parameter. +For example (see also [@../../test/identity.cpp =identity.cpp=]): + +[identity_make_checks] +[identity_make_call] + +[endsect] + +[endsect] + +[xinclude reference.xml] + +[section:Acknowledgments Acknowledgments] + +Many thanks to Mathias Gaunard for suggesting to implement [classref boost::overloaded_function] and for some sample code. + +Thanks to John Bytheway for suggesting to implement [funcref boost::make_overloaded_function]. + +Thanks to Nathan Ridge for suggestions on how to implement [funcref boost::make_overloaded_function]. + +Thanks to Robert Stewart for commenting on the library name. + +Many thanks to the entire __Boost__ community and mailing list for providing valuable comments about this library and great insights on the C++ programming language. + +[endsect] + diff --git a/overloaded_function/index.html b/overloaded_function/index.html new file mode 100644 index 0000000..00b3362 --- /dev/null +++ b/overloaded_function/index.html @@ -0,0 +1,15 @@ + + + + + + + Automatic redirection failed, click this + link  
+

© Copyright Lorenzo Caminiti, 2009-2012

+

Distributed under the Boost Software License, Version 1.0 (see + accompanying file + LICENSE_1_0.txt or a copy at + www.boost.org/LICENSE_1_0.txt)

+ + diff --git a/overloaded_function/test/Jamfile.v2 b/overloaded_function/test/Jamfile.v2 new file mode 100644 index 0000000..8856aac --- /dev/null +++ b/overloaded_function/test/Jamfile.v2 @@ -0,0 +1,13 @@ + +# Copyright (C) 2009-2012 Lorenzo Caminiti +# Distributed under the Boost Software License, Version 1.0 +# (see accompanying file LICENSE_1_0.txt or a copy at +# http://www.boost.org/LICENSE_1_0.txt) +# Home at http://www.boost.org/libs/functional/overloaded_function + +import testing ; + +project : requirements /boost//unit_test_framework ; + +run identity.cpp ; + diff --git a/overloaded_function/test/identity.cpp b/overloaded_function/test/identity.cpp new file mode 100644 index 0000000..7df5b7b --- /dev/null +++ b/overloaded_function/test/identity.cpp @@ -0,0 +1,70 @@ + +// Copyright (C) 2009-2012 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 +// (see accompanying file LICENSE_1_0.txt or a copy at +// http://www.boost.org/LICENSE_1_0.txt) +// Home at http://www.boost.org/libs/functional/overloaded_function + +#include +#include +#define BOOST_TEST_MODULE TestIdentity +#include +#include + +//[identity_make_checks +template +void check(F identity) { + BOOST_CHECK( identity("abc") == "abc" ); + BOOST_CHECK( identity(123) == 123 ); + BOOST_CHECK( identity(1.23) == 1.23 ); +} +//] + +//[identity_decls +const std::string& identity_s(const std::string& x) { return x; } // As pointer. + +int identity_i_impl(int x) { return x; } +int (&identity_i)(int) = identity_i_impl; // Function reference. + +double identity_d_impl(double x) { return x; } +boost::function identity_d = identity_d_impl; // Functor. +//] + +BOOST_AUTO_TEST_CASE( test_identity ) { + //[identity_calls + BOOST_CHECK( identity_s("abc") == "abc" ); + BOOST_CHECK( identity_i(123) == 123 ); + BOOST_CHECK( identity_d(1.23) == 1.23 ); + //] + + { + //[identity + boost::overloaded_function< + const std::string& (const std::string&) + , int (int) + , double (double) + > identity(identity_s, identity_i, identity_d); + + // All calls via single `identity` function. + BOOST_CHECK( identity("abc") == "abc" ); + BOOST_CHECK( identity(123) == 123 ); + BOOST_CHECK( identity(1.23) == 1.23 ); + //] + } + + { + //[identity_make + BOOST_AUTO(identity, boost::make_overloaded_function( + identity_s, identity_i, identity_d)); + + BOOST_CHECK( identity("abc") == "abc" ); + BOOST_CHECK( identity(123) == 123 ); + BOOST_CHECK( identity(1.23) == 1.23 ); + //] + } + + //[identity_make_call + check(boost::make_overloaded_function(identity_s, identity_i, identity_d)); + //] +} +