加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 使用类型别名的变量模板推导

发布时间:2020-12-16 06:50:17 所属栏目:百科 来源:网络整理
导读:我有一个这样的程序: templatetypename ...Argsusing Function = void(*)(Args *...);templatetypename ...Argsvoid DoThing(FunctionArgs... func) { }void IntFunction(int *i) { }int main(int argc,char *argv[]) { DoThing(IntFunction);} 当我运行程
我有一个这样的程序:

template<typename ...Args>
using Function = void(*)(Args *...);

template<typename ...Args>
void DoThing(Function<Args...> func) { }

void IntFunction(int *i) { }

int main(int argc,char *argv[]) {
  DoThing(IntFunction);
}

当我运行程序时,我收到此错误

$clang++ -std=c++14 template.cpp
template.cpp:12:3: error: no matching function for call to 'DoThing'
  DoThing(IntFunction);
  ^~~~~~~
template.cpp:7:6: note: candidate template ignored: substitution failure [with Args = int]
void DoThing(Function<Args...> func) { }
     ^
1 error generated.

但是如果我使用g编译它我不会得到任何错误.

当在类型别名中使用时,clang似乎无法推断可变参数模板参数.如果我用标准参数替换可变参数,那么我不会再得到错误.

哪个编译器给了我正确的结果?为什么我不被允许这样做?

解决方法

可以减少到

template <typename... T>
using funptr = void(*)(T...);

template <typename... T>
void f(funptr<T...>) {}

template void f(void(*)());

这是有效的代码;如果我们用funptr替换< T ...>通过相应的包装扩展,Clang突然不再抱怨了.

报道为#25250.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读