Currently my code has this form:
template <typename T>
class A {
private:
T data;
public:
void apply_process(A<T> obj, std::function<T(A<T>&)> process) {
data = process(obj);
}
// ...
};
This works with runtime lambdas; yet, while obj
will be only known on runtime,process
will be known at compile time. Can I rewrite this somehow so that process
is a metafunction to be passed as a compile-time parameter? A C 14 solution is preferable.
CodePudding user response:
It is not clear what you want, but by combining up the keywords you are throwing, I think you want this:
template <typename T>
class A {
private:
T data;
public:
template<class UnaryFunction>
void apply_process(A<T> obj, UnaryFunction process) {
data = process(obj);
}
// ...
};
to be a bit more pedantic use perfect forwarding (from comment):
void apply_process(A<T> obj, UnaryFunction&& process) {
data = std::forward<UnaryFunction>(process)(obj);
}