Saturday, May 26, 2018

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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...