locked
Why function object can not be used with ref classes?

    Question

  • The following code fails to compile:

    ref class s sealed
    {
    	void fun(const char *)
    	{
    	}
    
    	void foo()
    	{
    		auto f = std::bind(&s::fun, this, std::placeholders::_1);
    		f("foo");
    	}
    };

    I get this error:

    error C2064: term does not evaluate to a function taking 2 arguments

    Does someone have any idea on how to fix it?

     

     

     

    Thursday, October 4, 2012 5:58 AM

Answers

All replies

  • Hi,

    These codes work fine in native class. You can delete ref and sealed keyword and it build well.

    If you want to using bind function in Windows Runtime, you can try event and delegate in C++/CX
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh755799.aspx

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by Jesse Jiang Thursday, October 11, 2012 7:36 AM
    • Marked as answer by Jesse Jiang Tuesday, October 30, 2012 7:30 AM
    Friday, October 5, 2012 7:12 AM
  • Is it documented somewhere so that time of millions of other programmer is saved? Why does compiler allow to create the function objects with ref classes when we can not use them later? In other words, why does the second statement fails not the first one?
    Sunday, October 7, 2012 2:39 PM
  • Hi,

     

    As far as I know, to use Windows Runtime class which is ref class, it is necessary for exchange data with other Windows Runtime  components. Just like using C++/CLI to communication with .NET.

     

    Here is the document talking about Windows Runtime Components

    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh441569.aspx

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 10, 2012 6:35 AM