Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-06-09 09:54:52


The boost.function discussion got me thinking about how a class can support
the

if(f) {}

syntax, without unwanted consequences. Here's what I came up with:

#include <cstring>

struct X
{
 void f() {}
};

typedef void (X::*pmf)();

struct F
{
 operator pmf ()
 {
  return empty()? 0: &X::f;
 }

 bool empty()
 {
  return false;
 }
};

int main()
{
 F f;

 if(f) {}

 f == f;
 f != f;

// f - f;
// f + 4;
// f < f;
// *f;
// f[5];
// std::memcpy(&f, f, 4);
// delete f;
}

The commented lines do not compile. This leaves us only with operator==/!=
to worry about.

What do you think? :-)

--
Peter Dimov
Multi Media Ltd.

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