C++11 Template function that takes a std::function

2020-02-11 06:52发布

I am trying to write a template function that accepts a std::function which depends on the template arguments. Unfortunately the compiler is not capable of correctly deucing the arguments to the std::function. Here some simple example code:

#include <iostream>
#include <functional>

using namespace std;

void DoSomething( unsigned ident, unsigned param )
{
    cout << "DoSomething called, ident = " << ident << ", param = "  << param << "\n";
}

template < typename Ident, typename Param >
void CallFunc( Ident ident, Param param, std::function< void ( Ident, Param ) > op )
{
    op( ident, param );
}

int main()
{
    unsigned id(1);
    unsigned param(1);

    // The following fails to compile
    // CallFunc( id, param, DoSomething );

    // this is ok 
    std::function< void ( unsigned, unsigned ) > func( DoSomething );
    CallFunc( id, param, func ); 

    return 0;
}

If I call the template with the following:

CallFunc( id, param, DoSomething );

I get the following errors:

function-tpl.cpp:25: error: no matching function for call to CallFunc(unsigned int&, unsigned int&, void (&)(unsigned int, unsigned int))

If I explicitly create a std::function of the correct type (or cast it) the problem goes away:

std::function< void ( unsigned, unsigned ) > func( DoSomething );
CallFunc( id, param, func );

How would I code this so that the explicit temporary is not needed?

标签: c++ c++11
3条回答
够拽才男人
2楼-- · 2020-02-11 07:37

You can do the conversion inline or use bind. Neither is particularly pretty, but they get the job done:

CallFunc(id, param, std::function<void(unsigned, unsigned)>(DoSomething));

CallFunc(id, param, std::bind(DoSomething, std::placeholders::_1, std::placeholders::_2));

查看更多
放荡不羁爱自由
3楼-- · 2020-02-11 07:40

If you are using templates, you can avoid std::function entirely, unless for some reason you want to specifically restrict the function to take std::function:

template < typename Ident, typename Param, typename Func >
void CallFunc( Ident ident, Param param, Func op )
{
    op( ident, param );
}
查看更多
一夜七次
4楼-- · 2020-02-11 07:41

You need to make the third function parameter a non-deduced context for the template parameters therein. Then the compiler will not compare the argument type against the parameter type without also considering all implicit conversions (the Standard says, and C++0x clarified this further, that for a function parameter where there are no template parameters in deducing positions, all implicit conversions are allowed in bridging a difference).

template < typename T > struct id { typedef T type; };

template < typename Ident, typename Param >
void CallFunc( Ident ident, Param param, 
               typename id<std::function< void ( Ident, Param ) >>::type op )
{
    op( ident, param );
}

Instead of id you can use boost::identity. In C++0x and compilers that support it, you can have a more readable edition using alias templates

template < typename T > using nondeduced = typename id<T>::type;

Then your code becomes simply

template < typename Ident, typename Param >
void CallFunc( Ident ident, Param param, 
               std::function< nondeduced<void ( Ident, Param )> > op )
{
    op( ident, param );
}

However GCC does not yet support alias templates.

查看更多
登录 后发表回答