vendredi 24 juillet 2015

C++11 : type name for std::mem_fn

auto is good, but I need to declare a member in a class, not a variable in the stack.

decltype works, but just somehow looks weird

class Automation {
    void _init_state(int);

    decltype(std::mem_fn(&Automation::_init_state)) next_state;
};

std::function seems to work too, but there is a slight difference from pure member function

class Automation {
    void _init_state(int) {}
public:
    decltype(std::mem_fn(&Automation::_init_state)) next_state;
    std::function<void(Automation&, int)> next_state_fn;

    Automation()
        : next_state(&Automation::_init_state)
        , next_state_fn(&Automation::_init_state)
    {}
};

int main()
{
    /* on ubuntu, x64 */
    std::cout << sizeof Automation::next_state << std::endl; /* 16 */
    std::cout << sizeof Automation::next_state_fn << std::endl; /* 32 */
    return 0;
}

Could anybody please tell me what's the proper way for that?

Aucun commentaire:

Enregistrer un commentaire