user385261
user385261

Reputation: 4169

Is it possible to declare void pointer to a template function in C++?

Is it possible?

If as I have understood is correct, void pointer can point to any type. Therefore, a template function (undeclared type) is possible? or void pointer is only reserve for "variable" not function? Then what about void function pointer?

Upvotes: 0

Views: 686

Answers (4)

Necrolis
Necrolis

Reputation: 26171

to do this with templates you need some trickery, else the compiler cannot disambiguate the function (this is really not recommended, its horrible to read, and probably violates a few thousand porgamming best practices)

IE: this does not work (atleast under VS08 & GCC 3.5):

template <typename tType> tType* GetNULLType()
{
    return static_cast<tType*>(0);
}

void* pf = static_cast<void*>(GetNULLType<int>);

you instead need to do:

template <typename tType> tType* GetNULLType()
{
    return static_cast<tType*>(0);
}

typedef int* (*t_pointer)();
t_pointer pfGetNull = GetNULLType<int>;
void* pfVoid = (void*)(pfGetNull);

(and before purists moan, it seems C++ style 'safe' casting will not allow this)

Upvotes: 0

Matthieu M.
Matthieu M.

Reputation: 299890

There is an issue here, because of the words used I am afraid.

There is a difference between pointers and function pointers, most notably they need not be the same size.

Therefore it is undefined behavior to use void* type to hold the address of a function pointer.

In general it is not a good idea in C++ to use void*. Those are necessary in C because of the lack of a proper type system but C++ type system is much more evolved (even though not as evolved as recent languages).

You could probably benefit from some objectification here. If you make your method an instance of a class (template) you can have this class derived from a common base class. This is quite common, those objects are called Functors.

However, without a precise description of your issue, it'll be hard to help more.

Upvotes: 0

James Curran
James Curran

Reputation: 103515

According to the Standard, a void* is not required to be able to hold a function pointer. (It is required to hold a pointer to any kind of data). However, most cpu architectures you're likely to see these days have data pointers & function pointers that are the same size.

Upvotes: 0

Anthony Williams
Anthony Williams

Reputation: 68611

You can cast any function pointer type to any other, but you'd better cast it to the right type before you call it. You can therefore use void(*)() as an equivalent to void* for function pointers. This also works with function templates.

template<typename T>
void f(T){}

typedef void(*voidfp)();

voidfp fp=static_cast<voidfp>(&f<int>); // store address of f(int) in variable
static_cast<void(*)(int)>(fp)(3); // call the function

fp=static_cast<voidfp>(&f<std::string>); // store address of f(std::string) in variable
static_cast<void(*)(std::string)>(fp)("hello"); // call the function

Upvotes: 1

Related Questions