c++type-conversiontypedefcoercionsafe-bool-idiom

Are there cases where a typedef is absolutely necessary?


Consider the following excerpt from the safe bool idiom:

typedef void (Testable::*bool_type)() const;
operator bool_type() const;

Is it possible to declare the conversion function without the typedef? The following does not compile:

operator (void (Testable::*)() const)() const;

Solution

  • Ah, I just remembered the identity meta-function. It is possible to write

    operator typename identity<void (Testable::*)() const>::type() const;
    

    with the following definition of identity:

    template <typename T>
    struct identity
    {
        typedef T type;
    };
    

    You could argue that identity still uses a typedef, but this solution is "good" enough for me.