#include <complex>
#include <functional>
#include <iostream>
#include <vector>
using Complex = std::complex<double>;
using namespace std::literals::complex_literals;
struct S
{
std::vector<Complex> data;
S& apply(std::function<Complex(Complex const&)> f)
{
for( auto& elem : data )
{
elem = f(elem);
}
return *this;
}
};
int main()
{
S s{{1.i,-2.i,3.i}};
s.apply(std::conj); // error
return EXIT_SUCCESS;
}
error: cannot convert '<unresolved overloaded function type>' to 'std::function<std::complex<double>(const std::complex<double>&)>'
Now a single workaround would be to use a lambda or define a non-templated function. Is there another way how I can specify the template so I can pass std::conj
to std::function
?