Files
functional/overloaded_function/doc/overloaded_function.qbk
2012-02-15 01:11:29 +00:00

155 lines
8.1 KiB
Plaintext

[/ 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 <email>lorcaminiti@gmail.com</email>, 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]