【问题标题】:Passing lambda as template parameter to templated by function-pointer function将 lambda 作为模板参数传递给函数指针函数模板化
【发布时间】:2015-09-10 23:26:43
【问题描述】:

看起来我无法将无捕获 lambda 作为模板参数传递给由函数指针函数模板化的函数。是我做错了,还是不可能?

#include <iostream>

// Function templated by function pointer
template< void(*F)(int) >
void fun( int i )
{
    F(i);
}

void f1( int i )
{
    std::cout << i << std::endl;
}

int main()
{
    void(*f2)( int ) = []( int i ) { std::cout << i << std::endl; };

    fun<f1>( 42 ); // THIS WORKS
    f2( 42 );      // THIS WORKS
    fun<f2>( 42 ); // THIS DOES NOT WORK (COMPILE-TIME ERROR) !!!

    return 0;
}

【问题讨论】:

  • 使用std::function
  • f2 是一个变量 - 运行时参数。模板需要构建时间参数(常量和类型)。尝试添加 const,但它可能不起作用。

标签: templates c++11 lambda c++14 c++17


【解决方案1】:

这主要是语言定义中的一个问题,以下使其更加明显:

using F2 = void(*)( int );

// this works:
constexpr F2 f2 = f1;

// this does not:
constexpr F2 f2 = []( int i ) { std::cout << i << std::endl; };

Live example

这基本上意味着您的希望/期望是相当合理的,但语言目前没有以这种方式定义 - lambda 不会产生适合作为 constexpr 的函数指针。

不过,有一个解决此问题的建议:N4487

【讨论】:

    【解决方案2】:

    这是不可行的,因为f2 不是constexpr(即,是一个运行时变量)。因此,它不能用作模板参数。您可以通过以下方式更改代码并使其更通用:

    #include <iostream>
    
    template<typename F, typename ...Args>
    void fun(F f, Args... args) {
      f(args...);
    }
    
    void f1( int i ) {
        std::cout << i << std::endl;
    }
    
    int main() {
        auto f2 = []( int i ) { std::cout << i << std::endl; };
        fun(f1, 42);
        f2( 42 );
        fun(f2, 42 );
        return 0;
    }
    

    【讨论】: