用于存储指向 std::max 的函数指针的自动变量 [英] Auto variable to store function pointer to std::max

查看:26
本文介绍了用于存储指向 std::max 的函数指针的自动变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将函数 std::max 作为模板参数传递给模板化函数,但由于某些原因,编译器打印出无法推断函数类型的错误.一个简单的例子重现了同样的问题.它适用于自己的 max2 函数,但不适用于 STL std::max:

I'm trying to pass function std::max as template parameter to a templated function, but for some reasons compiler prints error that function type cannot be deduced. A simple example reproduces the same issue. It works with own max2 function but doesn't work with 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 不是一个单一的、明确的函数.此时是重载集,还有两种可能:

As seen here, std::max<float> isn't a single, unambiguous function. At this point, it's an overload set and there are still two possibilities:

constexpr const float& max( const float& a, const float& b );
constexpr float max( std::initializer_list<float> ilist );

您有两个主要选择:

  1. 将其包裹在 lambda 中:

  1. Wrap it in a lambda:

 auto f = [](float a, float b) { return std::max(a, b); };
 // Note there's no by-reference behaviour in this lambda.

  • 如果你想要一些更可重复使用的东西,你需要单独包装它,例如,作为不需要恶作剧传递的东西:

  • If you want something more reusable, you'll need to wrap it separately, e.g., as something that doesn't require shenanigans to pass around:

     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 宏.

    Obviously #2 comes with significant boilerplate, and that's ignoring other overloads and constexpr. In the future, it is expected that you will be able to do better. Today, you could emulate such a thing with a macro (most simply done by making the macro expand into a lambda). I've come across at least one LIFT macro that does this.

    第三个选项可能很吸引人,因为它是一行(丑陋的一行,但是是一行),并且正在转换为正确的函数指针类型.但是,这是不允许的,除非在一些特殊情况下 [namespace.std]/6.

    There's a third option that can be appealing because it's one line (an ugly line, but one line), and that's casting to the correct function pointer type. However, this isn't allowed except in a few special cases per [namespace.std]/6.

    这篇关于用于存储指向 std::max 的函数指针的自动变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆