Wskaźnik na funkcje i std::function - jak używać?

0
#include <functional>
#include <cstdio>

template < class T > class object
{
  public:
	using fn = void (T::*) ();

	void exec(fn f)
	{
		std::function < fn > func(T * &);
		func;
	}
};

class c:public object < c >
{
  public:
	void e()
	{
		printf("Hello");
	}
	c ()
	{
		exec(&c::e);
	}
};

int main()
{
	object < c > o;

	o.exec(&c::e);
}

Kompiluje sie, ale nie wyświetla tekstu. Co robić?

0
        std::function < fn > func(T * &);
        func;

Czego oczekujesz po tym kodzie? Deklarujesz tutaj funkcję przyjmującą referencję na wskaźnik na T, zwracającą std::function<fn>. Potem... nie wywołujesz tejże funkcji.

2
#include <iostream>
#include <functional>

int sum(int, int);

int main()
{
	using fp1 = int(*)(int, int);
	using fp2 = std::function<int(int, int)>;


	fp1 ptr1 = sum;
	fp2 ptr2 = sum;
	fp2 ptr3 = ptr1;

	std::cout << "ptr1 -> " << ptr1(1, 2) << "\nptr2 -> " << ptr2(3, 4) << "\nptr3 -> " << ptr3(5, 6);

	return 0;
}

int sum(int a, int b)
{
	return a + b;
}

https://wandbox.org/permlink/C9rJDyPEqRE8PdnN

1 użytkowników online, w tym zalogowanych: 0, gości: 1