c++ - Why member functions can't be used as template arguments? -
why member functions cannot used template arguments? example, want like:
struct foo { void bar() { // } }; template <typename towner, void(&func)()> void call(towner *p) { p->func(); } int main() { foo a; call<foo, foo::bar>(&a); return 0; } i know similar thing can done using pointers-to-member; well, it's cool enough of time, i'm curious why pointers "should" used.
i see no ambiguity of interpreting "p->func()" above. why standard prohibits use member functions template arguments? static member functions not allowed according compiler (vc++ 2013). know reason? or, there way same thing without loss of performance due pointer dereferencing?
thank you.
they can used non-type parameters, need use right syntax
struct foo { void bar() { // } }; template <typename towner, void(towner::*func)()> void call(towner *p) { (p->*func)(); } int main() { foo a; call<foo, &foo::bar>(&a); return 0; }
Comments
Post a Comment