0

Templates can take non-type function pointer parameters, but there is a problem if all possible function pointer parameters are accepted, example:

void dummy()
{
}

template <typename FT, FT* fp>
void proxy()
{
  fp();
}

int main()
{
  proxy<decltype(dummy), &dummy>();

  return 0;
}

As you can see, this is very cumbersome. Does there exist a more convenient way to provide a "wildcard" function pointer as a non-type template parameter?

user1095108
  • 14,119
  • 9
  • 58
  • 116
  • possible duplicate of [c++ deduction of "non type pointer to function" class template parameters](http://stackoverflow.com/questions/368737/c-deduction-of-non-type-pointer-to-function-class-template-parameters) – Chowlett Nov 08 '13 at 11:04
  • @Chowlett no, it's different. The question you reference narrows the kinds of function pointer one can provide. – user1095108 Nov 08 '13 at 11:09
  • @KerrekSB Mr. Watson, I can't use a function to do it, as a function argument can't be used to instantiate further templates. – user1095108 Nov 08 '13 at 11:11
  • @user1095108: Ah, an elementary oversight on my part. – Kerrek SB Nov 08 '13 at 11:22
  • 1
    Perhaps a macro would be a sane solution for this use case. – Kerrek SB Nov 08 '13 at 11:23
  • But - the linked question states that it's impossible for the narrower case. Therefore it's certainly impossible for the general case! – Chowlett Nov 08 '13 at 11:34
  • @Chowlett That was in the summer of '69, before `c++11`. – user1095108 Nov 08 '13 at 11:34

3 Answers3

1

A better solution for your particular problem would be to simply take only the function type as a template argument and the item as an ordinary function argument. You can also use type deduction instead of explicitly specifying which argument types are used:

void dummy()
{
}

template <typename FT>
void proxy(FT fp)
{
  fp();
}

int main()
{
  proxy(fp);

  return 0;
}
Agentlien
  • 4,996
  • 1
  • 16
  • 27
  • I need to retain the function pointer as a template parameter to instantiate other templates with it. – user1095108 Nov 08 '13 at 11:10
  • @user1095108 So other template methods are called from within `proxy()`, or template classes are instantiated? If the first, can those calls not also fall back on type deduction? If the second - hmm.... – Grimm The Opiner Nov 08 '13 at 11:23
  • @user1158692 a template function is instantiated, and, in addition to `FT` and `fp`, it knows the return type and the type of the arguments (since those get deduced). Knowing all this it can provide the arguments of the right type and return a correct value from `fp`. – user1095108 Nov 08 '13 at 12:03
0

If you know the signature of the function you wish to call (and surely you do), can't you do

template<void F()>
void proxy()
{
    F();
}

void dummy() {}

int main()
{
    proxy<dummy>();
}
Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
  • I don't know the signature. Any function pointer can be taken, there are no restrictions. – user1095108 Nov 08 '13 at 11:11
  • @user1095108 Hold on, you MUST know the function signature in order to invoke the function from within `proxy` (or anywhere else), as `proxy` (or wherever) must supply any parameters and of the right type!? Either that of the function is never invoked - in which case don't bother with any of it! ;-) – Grimm The Opiner Nov 08 '13 at 11:35
  • @user1158692 I can instantiate a template with `FT` and `fp`, and *then* it can deduce what kind the arguments and the return type are and provide them to `fp` when it is invoked in some way. It can obtain the arguments from anywhere it wants, a network, a database ... – user1095108 Nov 08 '13 at 11:59
0

Since c++17, we now have this:

void dummy() { }

template <auto fp>
void proxy()
{
  fp();
}

int main()
{
  proxy<dummy>();

  return 0;
}

You might want to check out the requires keyword as well.

user1095108
  • 14,119
  • 9
  • 58
  • 116