自动变量可存储函数指针到std :: max
Auto variable to store function pointer to std::max
我正在尝试将函数std::max
作为模板参数传递到模板函数,但由于某些原因,编译器打印错误的错误无法推导函数类型。一个简单的示例复制了同一问题。它可以使用自己的max2
函数,但不适用于STL std::max
:
#include <algorithm>
template <class T>
T max2(const T& a, const T& b) { return std::max(a, b); }
int main() {
#if 1
auto f = max2<float>;
#else
// error: unable to deduce ‘auto’ from ‘max<float>’
auto f = std::max<float>;
#endif
float max_val = f(1.0f, 2.0f);
return 0;
}
如下所示, std::max<float>
不是一个明确的函数。此时,这是一个过载组,仍然有两种可能性:
constexpr const float& max( const float& a, const float& b );
constexpr float max( std::initializer_list<float> ilist );
您有两个主要选择:
将其包裹在lambda中:
auto f = [](float a, float b) { return std::max(a, b); }; // Note there's no by-reference behaviour in this lambda.
如果您想要更可重复使用的东西,则需要单独包装,例如,不需要恶作剧来传播的东西:
struct max_fn { template<typename T> const T& operator()(const T& a, const T& b) const { return std::max(a, b); } };
显然#2带有重要的样板,这忽略了其他过载和constexpr
。将来,预计您将能够做得更好。今天,您可以用宏来模仿这样的东西(最简单地使宏扩展到lambda)。我遇到了至少一个LIFT
宏。
有一个第三个选项可以吸引人,因为它是一行(一条丑陋的行,但一行),并且铸造为正确的功能指针类型。但是,这是不允许的,除了在几个特殊情况下[namespace.std]/6。
相关文章:
- 如何从 std::atomic 中提取指针 T<T>?
- std::vector::迭代器是否可以合法地作为指针
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- 如何调用存储在指向"std::函数"的指针中的 lambda?
- 如何使用 std::make_shared 创建基类类型的智能指针?
- 如何将唯一指针的 std::vector 转换为原始指针的 std::span?
- 编译器不会使用 -std=c++11 编译智能指针
- 我无法使用C++指针指向类方法返回的 std::vector
- std::byte 指针应该用于指针算术吗?
- std::unordered_map::提取引用/指针失效
- 在 c++ 中生成字符串到 std::指针列表的映射
- 通过线程共享 std::指针的矢量
- 将STD ::指针列表与比较器的对象列表
- std::指针和remove_if列表
- 调用具有const键类型引用的std::指针集的count方法
- 搜索指向类的std::指针集
- const传播到std::指针数组
- c++使用std::指针列表对std::vector元素重新排序
- 返回指向std::指针列表的指针时发生ImplicitCast_错误