作为成员的函数模板-GCC与CLANG
function template as member - GCC vs. CLANG
考虑以下代码:
#include <type_traits>
#include <utility>
template <typename F>
class function
{
public:
// using function_type = typename std::decay<F>::type;
using function_type = F;
function(F func)
: function_(func)
{
}
private:
function_type function_;
};
template <typename F>
function<F> make_function(F&& func)
{
return function<F>(std::forward<F>(func));
}
double f1(double)
{
return 0.0;
}
template <typename T>
T f2(T)
{
return T();
}
int main()
{
// works in both cases
make_function(f1);
// needs decay (with CLANG)
make_function(f2<double>);
}
类function
旨在成为任何Callable
的简单包装器。该代码使用GCC编译良好(我在git存储库中测试了4.9.2和7.0.0 20160427)。然而,clang(3.5.0)抱怨道:
function.cpp:17:17: error: data member instantiated with function type 'function_type' (aka 'double (double)')
function_type function_;
^
function.cpp:55:2: note: in instantiation of template class 'function<double (double)>' requested here
make_function(f2<double>);
那么这是GCC的一个bug吗?如果变量是参数(在make_function
和构造函数中),但如果它是成员变量,为什么它不起作用?衰变(commed-out)在正确的位置吗?还是应该将其移动到make_function
?为什么如果我传递函数(f1
)或函数模板的显式实例化(f2
)会有区别?
正如评论者所指出的,这是Clang中的一个bug。
代码开始使用GCC 4.7.1(甚至可能是4.7?)和Clang 3.7进行编译:请参阅GodBolt。
相关文章:
- 奇怪的结构&GCC&clang(void*返回类型)
- 如何让 GCC/Clang 在保留标识符上出错
- Visual C++: MSVC vs. GCC+CLANG: 处理 lambda 捕获类成员变量,正确的方法是什么?
- GCC/CLang不同意模板模板参数的部分特化
- 在 gcc/clang (C++) 中获取函数范围之外的标签地址
- C++指向成员的指针的类内初始化会使 MSVC 失败(但 GCC/Clang 工作)
- ICC 中的 -O2 搞砸了汇编程序,ICC 中的 -O1 和 GCC / Clang 中的所有优化都很好
- 为什么GCC/Clang甚至在最高优化水平上也不会内联
- 在继承多个空类时,了解GCC/Clang vs MSVC2015之间的不同填充规则
- C++ MSVC/GCC/Clang编译器错误
- 这被认为是有效的C 11还是C 14?还是GCC/Clang弄错了
- GCC/Clang上的模板错误,但在MSVC上没有错误
- 如何求解与GCC/Clang不同的C 功能模板的MSVC处理(无法识别的模板定义)
- 忽略 gcc/clang 的"-Wmissing-braces"警告是否明智?
- GCC/Clang 返回元组时x86_64 C++ ABI 不匹配?
- 为什么根据 C++11 标准,std::seed_seq是不可复制的,为什么 gcc/clang 不符合?
- 我可以用更新的 gcc/clang 定位较旧的 Linux 吗?C++
- gcc/clang:错误:未知类名"异常";你是说'std::exception'
- 模板专业化和DLL:Visual Studio vs. (GCC / Clang)
- 使用GCC/CLANG跟踪代码膨胀的工具