【发布时间】:2022-01-30 06:05:31
【问题描述】:
#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;
}
错误:无法将 '
' 转换为 'std::function<:complex>(const std::complex &)>'
现在一个解决方法是使用 lambda 或定义非模板函数。是否有另一种方法可以指定模板以便将std::conj 传递给std::function?
【问题讨论】:
-
获取(几乎)任何标准函数的地址都是UB,所以无论如何你都需要包装它。
标签: c++ templates function-pointers