Boost logo

Boost :

Subject: Re: [boost] [function] function wrapping and exception safety recap
From: Emil Dotchevski (emil_at_[hidden])
Date: 2010-10-12 14:28:10


On Tue, Oct 12, 2010 at 11:08 AM, Daniel Walker
<daniel.j.walker_at_[hidden]> wrote:
> True, that can be done, but boost::function is still default
> constructible and can still be cleared through assigning 0. So, you
> end up back to the exception safety question of how operator() behaves
> when the wrapper is empty. As you suggest, you could have the behavior
> depend on how it was constructed, so sometimes boost::function has a
> strong exception guarantee, sometimes it has a nothrow guarantee (and
> presumably, it's a no-op when empty).

Here is some code to make sure we're talking about the same thing:

#include <new>
using namespace std;

extern void throw_exception();

template <class T>
class function
{
public:
  void (*f)();
  function():f(&throw_exception) { }
  function( nothrow_t ): f(0) { }
  void operator()() { f(); }
};

int main()
{
  function<int()> f(); //by default, you get link error.
  //function<int()> f(nothrow); //crash, but no link error.
  f();
}

It would have been nicer if op() didn't throw to begin with but
because that's how it has been, this should remain the default
behavior.

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk