Tech Off Thread

5 posts

[C++] What is the function type

Back to Forum: Tech Off
  • User profile image
    Renumbered

    Quick question

     

    What is the function type (i think it's called that) for this functor

    struct S_FUNC {
    void operator()(int i) const {
    }
    };
    
    S_FUNC func;
    
    guard<void (*)(int)> g(func);
    

    For some reason, vs2010 doesn't think it is "void (*)(int)"
    so if it's not that then what is it ?

     

  • User profile image
    Dexter

    Hmm, what's up with all this recent interest in functors and C++? (see http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong) Smiley

     

    What you need is:

    guard<void(int)> g(func);

    Note the missing (*), function objects are not pointer to functions.

  • User profile image
    Renumbered

    That's the thread that made me interested in it.

     

    Sorry for not providing enough info (as STL complained about)

    template <typename F>
    class guard {
        F m_func;
    public:
        explicit guard(F func) : m_func(func) {}
        ~guard() {
            m_func(123);
        }
    };

  • User profile image
    Dexter

    Sorry for not providing enough info (as STL complained about)

    No problem, anyway my answer isn't quite right. void(int) will only work with std::function, what std::function does to achieve this is close to magic Smiley

     

    In your case you'll have to stick to something simpler:

    guard<S_FUNC> g(func);

     

     

  • User profile image
    Renumbered


    6 hours ago, Dexter wrote

    No problem, anyway my answer isn't quite right. void(int) will only work with std::function, what std::function does to achieve this is close to magic



    That is good to know Tongue Out



    In your case you'll have to stick to something simpler:
    1
    guard<S_FUNC> g(func);

    Well now i feel stupid, for not thinking of that.
    It's quite obvious now when i see the solution.


    Damn tunnel vision !


    Thank you for the help.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.