0

What is the C++ (14) syntax to initialise a pointer to an instantiated template friend function? Minimal example below. Thanks.

template <class template_param>
struct TYPE
{
  friend TYPE foo ( TYPE &, TYPE & ) { TYPE res; return res; }
  friend TYPE bar ( TYPE &, TYPE & ) { TYPE res; return res; }
};

struct CLASS
{
  typedef TYPE<int> (*ptr_to_function) ( TYPE<int>, TYPE<int> );

  ptr_to_function *  ptr_to_foo = foo; // What syntax is required here, or elsewhere,
  ptr_to_function *  ptr_to_bar = bar; // to set pointers to functions above?

  void calculate () {
    procedure ( ptr_to_bar );
    procedure ( ptr_to_foo );
  }

  void procedure ( ptr_to_function * fun ) { /* Use foo or bar via fun */ }
};

Pete D.
  • 165
  • 1
  • 12
  • Why did 2 people vote to close this as 'unclear'? The question looks perfectly clear to me. – HolyBlackCat Jul 29 '19 at 22:58
  • An almost duplicate [question](https://stackoverflow.com/questions/29712393/function-pointer-to-class-template-member). – 1201ProgramAlarm Jul 29 '19 at 23:03
  • Why are there two examples of the code? And what's the question actually? I don't get it. – Nikos C. Jul 30 '19 at 01:17
  • Fixed code normally represents an answer and doesn't belong in the question section. You can "accept" an answer (the checkbox at the left). Or if you think additional information about how the answer worked for you will help someone else, maybe make a constructive edit to that answer (when you have enough reputation) or add your own answer. (Though this isn't actually "fixed" - it looks like compilers may be optimizing away the unused pointers, but write a non-inline function that needs them and you'll have a link error.) – aschepler Jul 30 '19 at 03:25
  • Yep, my answer doesn't seem to work, so I deleted it. http://coliru.stacked-crooked.com/a/75295dfbf3bc371e – HolyBlackCat Jul 30 '19 at 06:28

1 Answers1

2

Thanks to HolyBlackCat I have an answer to my syntax question. aschepler's observation simply leads to putting foo and bar in a separate .cpp file. The rest of the comments are unhelpful to a SO novice.

... and now altered back to use one file.

#include <iostream>

template <class T> struct TYPE;

template <class T> TYPE<T> foo(TYPE<T> &, TYPE<T> &);
template <class T> TYPE<T> bar(TYPE<T> &, TYPE<T> &);

template <class T>
struct TYPE
{
  friend TYPE<T> foo<> ( TYPE<T> &, TYPE<T> & );
  friend TYPE<T> bar<> ( TYPE<T> &, TYPE<T> & );
};

template <class T> TYPE<T> foo(TYPE<T> &, TYPE<T> &) { TYPE<T> res; std::cout << "1" << std::endl; return res; }
template <class T> TYPE<T> bar(TYPE<T> &, TYPE<T> &) { TYPE<T> res; std::cout << "2" << std::endl; return res; }

struct CLASS
{
  typedef TYPE<int> (*ptr_to_function) ( TYPE<int> &, TYPE<int> & );

  ptr_to_function  ptr_to_foo = foo<int>; // Initialise pointer to foo
  ptr_to_function  ptr_to_bar = bar<int>;

  void calculate () {
    procedure ( ptr_to_bar );
    procedure ( ptr_to_foo );
  }

  void procedure ( ptr_to_function fun )
  {
    TYPE<int> x;
    fun(x, x);
  }
};

int main()
{
    CLASS c;
    c.calculate();
}
Pete D.
  • 165
  • 1
  • 12
  • The function definitions should still be in the header file, or else only the one source file will be able to use them. They just need to be outside the class definition rather than defined at the `friend` declarations. – aschepler Jul 30 '19 at 11:55
  • Ok aschepler, so now it will work in one file. Code altered to give a clean solution. – Pete D. Jul 30 '19 at 12:43