4pie0
4pie0

Reputation: 29724

void pointer to template function

assume this is not template function managed by me:

template<class T, class U>
U templatefunc(T t){ //(...); }

can I have a function with default arguments being template functions of type

templatefunc<int,double> 

how to declare void pointer then, so I could do:

void _new_func( const void*=&templatefunc<int,double>)

I think no, because you can't have void pointer to function, it has to be function pointer, right?

previously it was:

void _new_func(const void* = boost::test_tools::check_is_close) 

but with boost 1.54 it is not OK, because check_is_close is template.

Upvotes: 1

Views: 1411

Answers (2)

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

The type of &templatefunc<int,double> is a pointer to function returning double and taking one argument of type int. The template arguments don't matter any more - templatefunc<int,double> is just the name of the (instantiated) function. So you could have:

void _new_func(double(*func)(int) = &templatefunc<int,double>)

However, it seems like you want to take functions that have varying argument and return types. In this case, you could have an overload for the case where you pass no argument:

template <typename T, typename U>
void _new_func(U(*func)(T)) {
}

void _new_func() {
  _new_func(&templatefunc<int,double>);
}

Now you can call _new_func() and it will pass the function pointer &templatefunc<int,double> to the templated version. When you want to call _new_func with a different instantiation of templatefunc, you can, for example, do:

__new_func(&templatefunc<float, double>);

Upvotes: 1

Pete Becker
Pete Becker

Reputation: 76315

A template function instantiation is a function, so anything you can do with a function you can also do with a template function instantiation. However, you can't portably store the address of a function in a void*, regardless of whether the function is an ordinary function or a template function instantiation.

Upvotes: 1

Related Questions