为什么这段代码是用gcc编译的,而不是用clang编译的
Why does this code compile with gcc but not with clang
此代码与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.
我认为这段代码是不正确的。在这种情况下,Log
的函数参数不能用于模板参数推导,因为该参数是非推导上下文。
根据标准中的[temp.dexpract.type],p5列出了未推导的上下文,p5.5表示:
无法进行参数推导的函数参数因为关联的函数参数是一个函数,或者一组过载功能(13.4),并且以下一个或多个适用:
和p5.5.3说:
作为参数提供的函数集包含一个或多个函数模板。
我的解释是,您有一个函数参数,其函数参数是一个函数(指向),而该函数是一个功能模板。
可以说,因为这不是一个过载集,所以这可能是未来允许的,但我认为标准并不能保证这种技术会起作用。
相关文章:
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- clang 插件:在编译过程中修改 AST
- 错误:使用 clang 没有可行的重载,使用 GCC 编译
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 通过自制软件(macOS)安装的clang++:编译错误
- 在使用 Clang 编译 DLL 时指定 DEF 文件
- 获取 clang++:错误:在编译文件系统库的代码时
- 如何在 clang 的自动会议中检查支持编译标志
- constexpr 使用 clang 编译 TensorFlow 时出错
- 编译包含指向模板函数的指针的初始值设定项列表时,gcc 出错,但 clang 不出错
- 如何更改 clang 的预编译头文件的路径
- 除了说明符神秘地破坏编译(Clang,GCC不同意)
- VS 代码显示编译 clang 的错误消息
- 同一段代码,可以用 xcode 或使用 g++ 编译,在终端中编译 clang++,不能使用 gcc 或 clang 编
- 简单的 constexpr 函数无法使用 GCC 编译(clang 没问题)
- 在 C++14 模式下为 libstdc++ 编译 clang 正则表达式程序会导致错误
- 如何在Windows上编译Clang
- 在Windows中编译Clang插件
- 是否可以在内存中使用Clang c++ API编译Clang源代码?