operator member_function_pointer_type() without typedef?

喜你入骨 提交于 2019-12-07 06:42:42

问题


Is it possible to make an operator member_function_pointer_type() without using typedefs (i.e. by specifying the type of the member function pointer inline)?

For example, when implementing the Safe Bool Idiom:

class Foo
{
    typedef void (Foo::*bool_type)() const;
public:
    operator bool_type() const;
};

is it possible to write out the type of bool_type directly when declaring the operator? If so, how?


回答1:


It seems that this is the only case where one cannot declare a (typecasting) operator without using a typedef.

Had it been another function name or another operator x, then it works fine:

class Foo 
{
    typedef void (Foo::*bool_type)() const;
public:
    operator bool_type() const;

// other syntax
    void (Foo::* some_func () const) () const;  // ok! named function
    void (Foo::* operator * () const) () const;  // ok! operator *
    void (Foo::* operator () const) () const;  // error! typecasting operator
};

Demo.



来源:https://stackoverflow.com/questions/17130368/operator-member-function-pointer-type-without-typedef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!