#include <iostream>
#include <functional>

using namespace std;

template<typename a, class derived>
class functor
{
public:
	virtual ~functor()
	{}
	template<typename b>
	derived map(const std::function<b(a)> &f)
	{
		// Here you take advantage of the CRTP
		return static_cast<derived*>(this)->map(f);
	}
};

template<typename l, typename r>
class either : public functor<r, either<l, r>>
{
public:
	template<typename b>
	either<l, b> map(const std::function<b(r)> &f)
	{
		cout << "In derived" << endl;
		return  either<l, b>();
	}
};

int main() 
{
	// pointer to base class points to a derived object
	functor<int, either<int, int>> *ff = new either<int, int>();
	// map function will call the method of the derived class
	ff->map<int>([](int k){ return 1; });

	return 0;
}