Skip to content

Instantly share code, notes, and snippets.

@hoditohod
Created December 2, 2015 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoditohod/2553baac3eec8c82f61d to your computer and use it in GitHub Desktop.
Save hoditohod/2553baac3eec8c82f61d to your computer and use it in GitHub Desktop.
Lambda expression holder - polymorphic version
#include <iostream>
#include <memory>
using namespace std;
// blank template class
template<typename T>
class Holder { };
// specialized one with call signature
template<typename RetType, typename... ArgsType>
class Holder<RetType(ArgsType...)>
{
struct InnerBase
{
virtual RetType call(ArgsType... args) = 0;
virtual ~InnerBase() = default;
};
template<typename T>
struct Inner : InnerBase
{
T t;
// creates a copy of t!
Inner(const T& t) : t(t) {}
virtual RetType call(ArgsType... args)
{
return t(args...);
}
};
unique_ptr<InnerBase> ptr;
public:
RetType call(ArgsType... args)
{
return ptr->call(args...);
}
template<typename T>
void set(const T& lambda)
{
unique_ptr<InnerBase> tmp(new Inner<T>(lambda));
ptr = move(tmp);
}
};
int main()
{
Holder<bool(int)> holder;
cout << boolalpha;
{
// both i, and the lambda exist in local scope, and
// die when the scope is closed
int local=5;
holder.set([local](int arg)->bool
{
cout << "captured: " << local
<< ", arg: " << arg
<< endl;
return local==arg;
});
}
bool result = holder.call(5);
cout << "equal: " << result << endl;
result = holder.call(1);
cout << "equal: " << result << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment