mercredi 24 juin 2015

Passing lambda as template parameter to templated by function pointer function

Looks like I cannot pass a no-capture lambda as a template parameter to a templated by function pointer function. Am I doing it the wrong way, or is it impossible?

#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;
}

Aucun commentaire:

Enregistrer un commentaire