my understanding is that greater_equal<double>
is a functor that has a signature of function<bool (double, double)>
, so I have tried to perform the following,
#include <functional>
#include <memory>
using namespace std;
int main() {
std::shared_ptr<std::function<bool (double, double)>> f;
auto f1 =make_shared<greater_equal<double>>();
auto f2 = make_shared<less_equal<double>>();
f = f1;
}
To my surprise this wont compile because,
'': cannot convert from 'const std::shared_ptrstd::greater_equal<double>' to 'std::shared_ptr<std::function<bool (double,double)>>'
I am not sure why,
what I need is similar to the spirit in the above snippet, I will have a member field of function, std::shared_ptr<std::function<bool (double, double)>>
and I need to assign either <greater_equal<double>
or less_equal<double>
base on some condition, how should I achieve this?
What you wrote would work if std::greater_equal<double>
and std::less_equal<double>
inherited from std::function<bool(double, double)>
, which they don't do.
But since std::function<bool(double, double)
is constructible from an instance of std::greater_equal<double>
(and less_equal
), you can do this instead:
auto f1 = std::make_shared<std::function<bool(double, double)>>(std::greater_equal<double>{});
But I don't think you want std::function
in the first place.
greater_equal<double>
is a functor that has a signature offunction<bool (double, double)>
Yes, its operator()
has (double, double)
parameters and returns bool
, but it's not related to std::function
.
std::function
is a wrapper class that can store any object it considers to be callable. It accepts slight variations in signature (e.g. even though less/greater_equal
accept parameters by const references, and you told std::function
that parameters should be passed by value, it still works), and it can store objects with a state (with data members). Those features come with an overhead.
Judging by
I need to assign either
greater_equal<double>
orless_equal<double>
base on some condition
you need none of those std::function
features.
You could rather use a function pointer (which requires signatures to match exactly, and can't store any state):
bool (*ptr)(double, double) = [](double a, double b)
{
return a <= b;
};