I have a code:
class Factory
{
public:
    template<typename ...Args>
    static void testFunc(Args&& ...args)
    {
        cout << "inside function";
    }
};
Is it possible to create a pointer to testFunc? All I can do is to define it like this:
// main
void(*pFunc)() = &Factory::testFunc;
pFunc();
This works, but I can't pass variable number of arguments, like this:
void(*pFunc)(WHAT_TO_TYPE_HERE?) = &Factory::testFunc;
pFunc(10, false, 'a', 11.5);
P.S.: with ellipsis (...) everythings works.
Is it possible to create a pointer to
testFunc?
The declaration of testFunc defines a family of functions, parameterized by Args. Each of those functions will have its own memory address. So you can only have a pointer to a particular one of the functions defined by testFunc, such as testFunc<int, double> or testFunc<>.
Instead of writing the following to get a pointer to the 0-argument overload of testFunc
void(*pFunc)() = &Factory::testFunc;
you should be writing
void(*pFunc)() = &Factory::testFunc<>;
because Factory::testFunc without the <> is an overload set, not a function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With