【发布时间】:2016-05-27 22:16:34
【问题描述】:
此代码与 gcc/g++ 和 msvc 完美结合,但不适用于 clang。 一直报找不到Log的匹配函数,这是怎么回事?
#include <iostream>
template <typename Function, typename... Args>
auto Call(Function func, Args&&... args) -> typename std::result_of<Function&(Args&&...)>::type
{
return func(std::forward<Args>(args)...);
}
template <typename T, typename... Args>
T (*Log( T (*FuncPtr)(Args...) ))(Args...)
{
return FuncPtr;
}
int main()
{
auto r = Log(Call<int(int), int>)([](int x){
return x*10;
}, 10);
std::cerr << r << std::endl;
}
错误:
> error: no matching function for call to 'Log'
> auto r = Log(Call<int(int), int>)([](int x){
> ^~~ test7.cpp:15:5: note: candidate template ignored: couldn't infer template argument 'T' T (*Log( T (*FuncPtr)(Args...)
> ))(Args...)
> ^ 1 error generated.
【问题讨论】:
-
更简单:
auto call = Call<int(int), int>;失败,error: variable 'call' with type 'auto' has incompatible initializer of type '<overloaded function type>'。Log的问题是一个红鲱鱼,问题开始得更早 - 编译器似乎无法确定Call<int(int), int>是什么类型。 -
如果将
result_ofdance 替换为decltype(func(args...)),则可以使用 -
为什么会有一个&符号
std::result_of<Function &( ...)>?这对我来说看起来格格不入。 -
更好的是,将
typename std::result_of<...>::type替换为std::result_of_t<...>并clang 编译它。这……很奇怪。 -
@ChrisBeck 我扔掉了27918,我们来看看聪明人怎么说。