2

Can you explain for me what the typedef here is doing and what the purpose is?

class C
{
    public:
        ...

        typedef bool (C::*implementation_defined_bool_type)(bool) const;

        operator implementation_defined_bool_type() const {
            return _spi ? &C::isPersistent : 0;
        }

};
Baz
  • 12,713
  • 38
  • 145
  • 268
  • 2
    It's an implementation of the obsolescent ["safe bool" idiom](http://stackoverflow.com/questions/6242768/is-the-safe-bool-idiom-obsolete-in-c11). – CB Bailey Jun 19 '12 at 10:21

1 Answers1

5

Can you explain for me what the typedef is doing here?

typedef bool (C::*implementation_defined_bool_type)(bool) const;

typedefs a pointer to a const member function of a type C, which takes a bool as input parameter and also returns a bool.

While,

operator implementation_defined_bool_type() const 

Takes in an object of type C and returns a type implementation_defined_bool_type.
It is known as an Conversion Operator.

what is the purpose of it?

It implements the "Safe Bool Idiom", which aims to validate an object in a boolean context.
Note that the Safe Bool Idiom is obsolete with the C++11 Standard.

Good Read:
The Safe Bool Idiom

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • It doesn't explain what it actually does and why it is implemented that way, it's purpose and all. – Nawaz Jun 19 '12 at 10:25