处理<未解析的重载函数类型>将函数指针传递给函数时 [英] Dealing with <unresolved overloaded function type> when passing function pointer to function

查看:61
本文介绍了处理<未解析的重载函数类型>将函数指针传递给函数时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们考虑:

  void goo(){
std :: cout<< void goo()\n;
}

int goo(int){
std :: cout<< int goo(int)\n;
返回42;
}

现在我想使用定义的一些包装函数来调用其中一个函数

  template< typename F,typename ... A> 
void c(F& f,A& ... a){
f(std :: forward< A>(a)...);
}

用法:

  c(&go,10); //(X)
c(& goo); //(Y)

这两种情况均失败(GCC 5.3.1),并带有适当的错误:

 错误:没有匹配的函数调用'c(< unresolved重载函数类型> ;, int)'
错误:否匹配函数来调用'c(< unresolved重载函数类型>)'

担心失败是因为编译器必须初始化 f 对象(信息太少)时无法选择适当的重载。



作为一种解决方案,我当然可以这样编写用法调用:

  c(static_cast< int(*)(int )>(& goo,10); 
c(static_cast< void(*)()>(& goo));

告诉编译器我确实要使用哪个指针。



为我编写此 static_cast 会使代码更难看,所以我写了一个包装函数,用于将函数指针转换为使用模板的合适模板:

  template< typename R,typename ... Args> 
使用Funptr_t = R(*)(Args ...);

模板< typename R,typename ... Args>
Funptr_t< R,Args ...> funptr(Funptr_t< R,Args ...> ptr){
return static_cast< Funptr_t< R,Args ...>> (ptr);
}

现在使用情况看起来更好:

  c(funptr< int,int>(& goo),10); 
c(funptr< void>(& goo));

我的问题是:您有更好的主意如何应对这种情况吗?我敢肯定,这种情况经常在通用代码中发生。因此,请给我一些建议。



理想的解决方案是,如果我可以直接使用(X)和(Y),那么指向适当的重载的魔术将是使用 A ... 完成并隐藏给调用者。

解决方案

我的意思是,您总是可以显式指定模板参数:

  c< int(int)>(& goo,10 ); 
c< void()>(& goo);

AFAIK无法在 c ,因为无法推断 F (重载之间的歧义),并且要使编译器推断出正确的类型,您需要在调用方进行更改。 p>

Let's consider:

void goo () {
    std::cout << "void goo ()\n";
}   

int goo (int) {
    std::cout << "int goo (int)\n";
    return 42;
}

And now I want to call one of those functions using some wrapper function defined like this:

template <typename F, typename... A>
void c (F&& f, A&&... a) {
    f (std::forward<A> (a)...);
}

With usage:

c (&goo, 10); // (X)
c (&goo);     // (Y)

Both cases fail (GCC 5.3.1) with appropriate errors:

error: no matching function for call to ‘c(<unresolved overloaded function type>, int)’
error: no matching function for call to ‘c(<unresolved overloaded function type>)’

As far as I am concerned the fail is because compiler could not choose appropriate overload when it has to initialize f object (too few information).

As a solution of course I can write usage calls like this:

c (static_cast<int (*) (int)> (&goo), 10);
c (static_cast<void (*) ()> (&goo));

To tell the compiler which pointer I really want to use.

Writing this static_cast for me makes code much more uglier, so I wrote a wrapper function for converting function pointer to appropriate one using template:

template <typename R, typename... Args>
using Funptr_t = R (*) (Args...);

template <typename R, typename... Args>
Funptr_t<R, Args...> funptr (Funptr_t<R, Args...> ptr) {
    return static_cast<Funptr_t<R, Args...>> (ptr);
}

and now usage looks much better:

c (funptr<int, int> (&goo), 10);
c (funptr<void> (&goo));

My question is: Do you have any better idea how to deal with that kind of situation? I am pretty sure this happen very often in generic code. So, please advice me something.

The ideal solution would be if I could use (X) and (Y) directly, so the magic trick with pointing appropriate overload would be done using A... and hidden to the caller.

解决方案

I mean, you can always specify the template parameter explicitly:

c<int(int)>(&goo, 10);
c<void()>(&goo);

AFAIK there is no way to do that in c, because F cannot be deduced (ambiguity between the overloads), and to make the compiler deduce the correct type, you need to change something on the caller side.

这篇关于处理&lt;未解析的重载函数类型&gt;将函数指针传递给函数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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