Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/function/doc/faq.xml @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 7.3 KB
Line 
1<?xml version="1.0" encoding="utf-8"?>
2<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
3  "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
4<section id="function.faq" last-revision="$Date: 2004/02/18 06:37:13 $">
5  <title>Frequently Asked Questions</title>
6
7<qandaset>
8  <qandaentry>
9    <question><para>Why can't I compare
10    <classname>boost::function</classname> objects with
11    <code>operator==</code> or
12    <code>operator!=</code>?</para></question>
13
14    <answer>
15      <para>Comparison between <classname>boost::function</classname>
16      objects cannot be implemented "well", and therefore will not be
17      implemented. The typical semantics requested for <code>f ==
18      g</code> given <classname>boost::function</classname> objects
19      <code>f</code> and <code>g</code> are:</para>
20        <itemizedlist>
21          <listitem><simpara>If <code>f</code> and <code>g</code>
22          store function objects of the same type, use that type's
23          <code>operator==</code> to compare
24          them.</simpara></listitem> 
25
26          <listitem><simpara>If <code>f</code> and <code>g</code>
27          store function objects of different types, return
28          <code>false</code>.</simpara></listitem>
29        </itemizedlist>
30      <para>The problem occurs when the type of the function objects
31      stored by both <code>f</code> and <code>g</code> doesn't have an
32      <code>operator==</code>: we would like the expression <code>f ==
33      g</code> to fail to compile, as occurs with, e.g., the standard
34      containers. However, this is not implementable for
35      <classname>boost::function</classname> because it necessarily
36      "erases" some type information after it has been assigned a
37      function object, so it cannot try to call
38      <code>operator==</code> later: it must either find a way to call
39      <code>operator==</code> now, or it will never be able to call it
40      later. Note, for instance, what happens if you try to put a
41      <code>float</code> value into a
42      <classname>boost::function</classname> object: you will get an
43      error at the assignment operator or constructor, not in
44      <code>operator()</code>, because the function-call expression
45      must be bound in the constructor or assignment operator.</para>
46
47      <para>The most promising approach is to find a method of
48      determining if <code>operator==</code> can be called for a
49      particular type, and then supporting it only when it is
50      available; in other situations, an exception would be
51      thrown. However, to date there is no known way to detect if an
52      arbitrary operator expression <code>f == g</code> is suitably
53      defined. The best solution known has the following undesirable
54      qualities:</para>
55
56      <orderedlist>
57        <listitem><simpara>Fails at compile-time for objects where
58        <code>operator==</code> is not accessible (e.g., because it is
59        <code>private</code>).</simpara></listitem>
60
61        <listitem><simpara>Fails at compile-time if calling
62        <code>operator==</code> is ambiguous.</simpara></listitem>
63
64        <listitem><simpara>Appears to be correct if the
65        <code>operator==</code> declaration is correct, even though
66        <code>operator==</code> may not compile.</simpara></listitem>
67      </orderedlist>
68
69      <para>All of these problems translate into failures in the
70      <classname>boost::function</classname> constructors or
71      assignment operator, <emphasis>even if the user never invokes
72      operator==</emphasis>. We can't do that to users.</para>
73
74      <para>The other option is to place the burden on users that want
75      to use <code>operator==</code>, e.g., by providing an
76      <code>is_equality_comparable</code> trait they may
77      specialize. This is a workable solution, but is dangerous in
78      practice, because forgetting to specialize the trait will result
79      in unexpected exceptions being thrown from
80      <classname>boost::function</classname>'s
81      <code>operator==</code>. This essentially negates the usefulness
82      of <code>operator==</code> in the context in which it is most
83      desired: multitarget callbacks. The
84      <libraryname>Signals</libraryname> library has a way around
85      this.</para>
86    </answer>
87  </qandaentry>
88
89  <qandaentry>
90    <question><para>I see void pointers; is this [mess] type safe?</para></question>
91    <answer>
92<para>Yes, <computeroutput>boost::function</computeroutput> is type
93safe even though it uses void pointers and pointers to functions
94returning void and taking no arguments. Essentially, all type
95information is encoded in the functions that manage and invoke
96function pointers and function objects. Only these functions are
97instantiated with the exact type that is pointed to by the void
98pointer or pointer to void function. The reason that both are required
99is that one may cast between void pointers and object pointers safely
100or between different types of function pointers (provided you don't
101invoke a function pointer with the wrong type).  </para>
102    </answer>
103  </qandaentry>
104
105  <qandaentry>
106    <question><para>Why are there workarounds for void returns? C++ allows them!</para></question>
107    <answer><para>Void returns are permitted by the C++ standard, as in this code snippet:
108<programlisting>void f();
109void g() { return f(); }</programlisting>
110    </para>
111
112    <para> This is a valid usage of <computeroutput>boost::function</computeroutput> because void returns are not used. With void returns, we would attempting to compile ill-formed code similar to:
113<programlisting>int f();
114void g() { return f(); }</programlisting>
115</para>
116
117<para> In essence, not using void returns allows
118<computeroutput>boost::function</computeroutput> to swallow a return value. This is
119consistent with allowing the user to assign and invoke functions and
120function objects with parameters that don't exactly match.</para>
121
122    </answer>
123  </qandaentry>
124
125  <qandaentry>
126    <question><para>Why (function) cloning?</para></question>
127    <answer>
128      <para>In November and December of 2000, the issue of cloning
129      vs. reference counting was debated at length and it was decided
130      that cloning gave more predictable semantics. I won't rehash the
131      discussion here, but if it cloning is incorrect for a particular
132      application a reference-counting allocator could be used.</para>
133    </answer>
134  </qandaentry>
135
136  <qandaentry>
137    <question><para>How much overhead does a call through <code><classname>boost::function</classname></code> incur?</para></question>
138    <answer>
139      <para>The cost of <code>boost::function</code> can be reasonably
140      consistently measured at around 20ns +/- 10 ns on a modern >2GHz
141      platform versus directly inlining the code.</para>
142
143      <para>However, the performance of your application may benefit
144      from or be disadvantaged by <code>boost::function</code>
145      depending on how your C++ optimiser optimises.  Similar to a
146      standard function pointer, differences of order of 10% have been
147      noted to the benefit or disadvantage of using
148      <code>boost::function</code> to call a function that contains a
149      tight loop depending on your compilation circumstances.</para>
150     
151      <para>[Answer provided by Matt Hurd. See <ulink url="http://article.gmane.org/gmane.comp.lib.boost.devel/33278"/>]</para>
152    </answer>
153  </qandaentry>
154</qandaset> 
155
156</section>
Note: See TracBrowser for help on using the repository browser.