Friday, 5 August 2016

c++ - Call member method of a variadic class template with a member field

I learned a bit about variadic templates and searched over the Internet for some samples and now trying to write some tricky code to call member a method of a variadic class template with one of its fields. I can't understand why it doesn't work. Please, help.



Here is sample classes:



class BarBase

{
public:
BarBase() = default;

virtual void call() = 0;
};

template
class Bar
: public BarBase

{
public:
Bar(O* o, M m, A&&... a)
: BarBase()
, m_o(o), m_m(m), m_a(std::forward(a)...)
{ }

void call() override final
{
callInnerWithArgsInside();

}

private:
void callInnerWithArgsInside()
{
(m_o->*m_m)(m_a); // Some errors happends here
}

O* m_o;
M m_m;

std::tuple::type...> m_a;
};

template
BarBase* crateBar(O* o, M m, A&&... a)
{
return new Bar(o, m, std::forward
(a)...);
}



And call from main:



struct Foo
{
void foo(int ii, float ff, std::string ss)
{
std::cout << "called" << std::endl;
}
};


int main()
{
Foo f;
int i = 10;
float ff = 20.2f;
std::string s = "Hello";

BarBase* bar = crateBar(&f, &Foo::foo, i, ff, s);
bar->call();
}



Errors:



main.cpp



1>d:\drafts_tests\main.cpp(203): error C2198: 'void (__thiscall Foo::* )(int,float,std::string)' : too few arguments for call



1> d:\drafts_tests\main.cpp(202) : while compiling class template member function 'void Bar::callInnerWithArgsInside(void)'




1> with



1> [



1> O=Foo



1> , M=void (__thiscall Foo::* )(int,float,std::string)



1> ]




1> d:\drafts_tests\main.cpp(197) : see reference to function template instantiation 'void Bar::callInnerWithArgsInside(void)' being compiled



1> with



1> [



1> O=Foo



1> , M=void (__thiscall Foo::* )(int,float,std::string)




1> ]



1> d:\drafts_tests\main.cpp(214) : see reference to class template instantiation 'Bar' being compiled



1> with



1> [



1> O=Foo




1> , M=void (__thiscall Foo::* )(int,float,std::string)



1> ]



1> d:\drafts_tests\main.cpp(225) : see reference to function template instantiation 'BarBase *crateBar(O *,M,int &,float &,std::string &)' being compiled



1> with



1> [




1> O=Foo



1> , M=void (__thiscall Foo::* )(int,float,std::string)



1> ]



========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...