Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to variadic template static function. How?

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.

like image 240
nikitablack Avatar asked Mar 18 '23 20:03

nikitablack


1 Answers

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.

like image 91
Timothy Shields Avatar answered Mar 20 '23 14:03

Timothy Shields