Beyond the C++ Standard Library: An Introduction to Boost [Electronic resources] نسخه متنی

This is a Digital Library

With over 100,000 free electronic resource in Persian, Arabic and English

Beyond the C++ Standard Library: An Introduction to Boost [Electronic resources] - نسخه متنی

Bjorn Karlsson

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید

Function


Header: "boost/function.hpp"


The header "function.hpp" includes prototypes for functions with 0 to 10 arguments. (This is implementation defined, but 10 is the default limit for the current implementation.[1]) It is also possible to include only the header that corresponds to the number of arguments you need to usethe files are named "function/functionN.hpp", where N is in the range 0 to 10. There are two different interfaces for Boost.Function, one that is most appealing because it is syntactically close to a function declaration (and doesn't require the signature to include the number of arguments), and the other is appealing because it works with more compilers. Which to choose depends, at least in part, on the compiler that you are using. If you can, use what we refer to as the preferred syntax. Throughout this chapter, both forms will be used.

[1] Boost.Function can be configured to support up to 127 arguments.

Declarations Using the Preferred Syntax


A declaration of a function includes the signature and return type of the function or function object that the function is to be compatible with. The type of the result and the arguments are all supplied as a single argument to the template. For example, the declaration of a function that returns bool and accepts an argument of type int looks like this:


boost::function<bool (int)> f;

The argument list is supplied inside the parentheses, separated by commas, just like a function declaration. Thus, declaring a function that returns nothing (void) and takes two arguments, of type int and double, looks like this:


boost::function<void (int,double)> f;

Declarations Using the Compatible Syntax


The second way of declaring functions is to supply separate template type arguments for the return type and the argument types for the function call. Also, there's a suffix for the name of the function class, which is an integer that corresponds to the number of arguments the function will accept. For example, the declaration of a function that returns bool and accepts an argument of type int looks like this:


boost::function1<bool,int> f;

The numbering is based on the number of arguments that the function acceptsin the preceding example, there is one argument (int) and therefore function1 is needed. More arguments simply means supplying more template type parameters to the template and changing the numeric suffix. A function that returns void and accepts two arguments of type int and double looks like this:


boost::function2<void,int,double> f;

The library actually consists of a family of classes, each taking a different number of arguments. There is no need to take this into account when including the header "function.hpp", but if including the numbered versions, you must include the correct numbered header.

The preferred syntax is easier to read and is analogous to declaring a function, so you should use it when you can. Unfortunately, although the preferred syntax is perfectly legal C++ and easier to read, not all compilers support it as yet. If your compiler is among those that cannot handle the preferred syntax, you need to use the alternative form. If you need to write your code with maximum portability, you might also choose to use the alternative form. Let's take a look at the most important parts of a function's interface.

Members



function();

The default constructor creates an empty function object. If an empty function is invoked, it throws an exception of type bad_function_call.


template <typename F> function(F g);

This parameterized constructor accepts a compatible function objectthat is, a function or a function object that has a signature with a return type that is the same as, or implicitly convertible to, that of the function being constructed, and arguments the same as, or implicitly convertible to, that of the function being constructed. Note that another instance of function can also be used for construction. If that is the case, and the function f is empty, the constructed function will also be empty. This also applies to null function pointers and null pointers to membersthe resulting function is empty.


template <typename F> function(reference_wrapper<F> g);

This constructor is similar to the previous version, but takes its function object wrapped in a reference_wrapper, which is used to avoid passing by value, and thus creating a copy of the function or function object. The requirements on the function objects are that they be compatible with the signature of the function.


function& operator=(const function& g);

The copy assignment operator stores a copy of g's stored function or function object; if g is empty, the function being assigned to will also be empty.


template<typename F> function& operator=(F g);

The parameterized assignment operator accepts a compatible function pointer or function object. Note that another instance of function (with a different but compatible signature) can also be used for assignment. This also means that the function can be empty after assignment, which is the case if g is another instance of function and is empty. Assigning a null function pointer or a null pointer to member effectively empties the function.


bool empty() const;

This member returns a Boolean value that tells whether the function contains a function/function object or if it's empty. It returns false if there is a targeted function or function object that can be invoked. Because a function can already be tested in a Boolean context, or compared to 0, this member function may be deprecated in future versions of this library, so you might want to avoid it.


void clear();

This member function clears the function, which means that it is no longer targeting a function or function object. If the function is already empty, the call has no effect. After the call, the function is always empty. The preferred way to make a function empty is to assign 0 to it; clear may be deprecated in a future release of this library.


operator safe_bool() const

This conversion function returns an unspecified type (represented by safe_bool) that can be used in Boolean contexts. If the function is empty, the returned value is false. If the function is storing a function pointer or function object, the returned value is true. Note that using a type that is different from bool enables this conversion operator to be completely safe and not interfere with overloading, while still providing the idiomatic use of testing an instance of function directly in a Boolean context. It is also equivalent to the expression !!f, where f is an instance of function.


result_type operator()(Arg1 a1, Arg2 a2, ..., ArgN aN) const;

The function call operator is how a function is invoked. You cannot invoke an empty function or it will throw a bad_function_call exceptionthat is, !f.empty(), if (f), or if (!!f) yields true. The invocation results in calling the function or function object in the function, and returns its result.

/ 124