Can't bind method to tr1 function with no arguments

The following contains a snippet of code I representative of service provider I'm trying to implement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <string>
#include <tr1/functional>

class Service
{
public:
    virtual ~Service(){}
};

class ServiceProvider
{
public:
    typedef std::tr1::function<Service* ()> InvokeMethod;

    static void Register(const std::string&, InvokeMethod)
    {
        // maps string to method ...
    }
};

class MockTestService : public Service
{
public:
    MockTestService()
    {
        ServiceProvider::Register("MockTestService",
            std::tr1::bind(&MockTestService::GetService, this,
                std::tr1::placeholders::_1));
    }
    
private:
    Service* GetService()
    {
        return this;
    }
};


When I try to compile this code on Mac OS X 10.6 using version4.2.1 of the GNU compiler, I get a lengthy error message:

/usr/include/c++/4.2.1/tr1/bind_iterate.h: In member function ‘typename std::tr1::result_of<_Functor ()(typename std::tr1::result_of<std::tr1::_Mu<_T1, std::tr1::is_bind_expression<_T1>::value, (std::tr1::is_placeholder<_T1>::value > 0)> ()(_T1, std::tr1::tuple<std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass>)>::type, typename std::tr1::result_of<std::tr1::_Mu<_T1, std::tr1::is_bind_expression<_T1>::value, (std::tr1::is_placeholder<_T1>::value > 0)> ()(_T2, std::tr1::tuple<std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass>)>::type)>::type std::tr1::_Bind<_Functor ()(_T1, _T2)>::operator()() [with _Functor = std::tr1::_Mem_fn<Service* (MockTestService::*)()>, _T1 = MockTestService*, _T2 = std::tr1::_Placeholder<1>]’:
/usr/include/c++/4.2.1/tr1/functional_iterate.h:488:   instantiated from ‘static _Res std::tr1::_Function_handler<_Res ()(), _Functor>::_M_invoke(const std::tr1::_Any_data&) [with _Res = Service*, _Functor = std::tr1::_Bind<std::tr1::_Mem_fn<Service* (MockTestService::*)()> ()(MockTestService*, std::tr1::_Placeholder<1>)>]’
/usr/include/c++/4.2.1/tr1/functional_iterate.h:847:   instantiated from ‘std::tr1::function<_Res ()()>::function(_Functor, typename __gnu_cxx::__enable_if<(! std::tr1::is_integral<_Functor>::value), std::tr1::function<_Res ()()>::_Useless>::__type) [with _Functor = std::tr1::_Bind<std::tr1::_Mem_fn<Service* (MockTestService::*)()> ()(MockTestService*, std::tr1::_Placeholder<1>)>, _Res = Service*]’
ServiceProvider.cpp:28:   instantiated from here
/usr/include/c++/4.2.1/tr1/bind_iterate.h:45: error: no match for call to ‘(std::tr1::_Mem_fn<Service* (MockTestService::*)()>) (MockTestService*&, std::tr1::_NullClass&)’
/usr/include/c++/4.2.1/tr1/functional_iterate.h:207: note: candidates are: _Res std::tr1::_Mem_fn<_Res (_Class::*)()>::operator()(_Class&) const [with _Res = Service*, _Class = MockTestService]
/usr/include/c++/4.2.1/tr1/functional_iterate.h:213: note:                 _Res std::tr1::_Mem_fn<_Res (_Class::*)()>::operator()(_Class*) const [with _Res = Service*, _Class = MockTestService]
...

If I change the typedef of ServiceProvider::InvokeMethod to something like:
typedef std::tr1::function<Service* (void*)> InvokeMethod;
and make a simpilar change to the signature of MockTestService::GetService like:
Service MockTestService::GetService(void*);
the code compiles just fine.

How can I get the code to compile successfully without requiring any arguments for the callback type?
remove the std::tr1::placeholders::_1 from line 28. You're telling it that GetService should be passed one argument
to be determined at the point of invocation, but your InvokeMethod expects zero parameters.
Topic archived. No new replies allowed.