除了说明符神秘地破坏编译(Clang,GCC不同意)
noexcept specifier mysteriously breaks compilation (clang, gcc disagree)
有问题的代码是
#include <functional>
#include <utility>
template <typename F>
void for_each(F&&) noexcept {}
template <typename F, typename T, typename... Us>
void for_each(F&& f, T&& v, Us&&... us) {
std::invoke(std::forward<F>(f), std::forward<T>(v));
for_each(std::forward<F>(f), std::forward<Us>(us)...);
}
void func(void*) noexcept {}
int main() {
for_each(func, nullptr);
}
它在 gcc 8 上编译,但在 clang 6 上失败,并出现以下错误:
/opt/wandbox/clang-6.0.0/include/c++/v1/type_traits:4198:19: error: invalid application of 'sizeof' to a function type
static_assert(sizeof(_Tp) > 0, "Type must be complete.");
^~~~~~~~~~~
/opt/wandbox/clang-6.0.0/include/c++/v1/type_traits:4203:15: note: in instantiation of template class 'std::__1::__check_complete<void (void *) noexcept>' requested here
: private __check_complete<_Tp>
^
/opt/wandbox/clang-6.0.0/include/c++/v1/type_traits:4496:15: note: in instantiation of template class 'std::__1::__check_complete<void (&)(void *) noexcept>' requested here
: private __check_complete<_Fp>
^
/opt/wandbox/clang-6.0.0/include/c++/v1/type_traits:4559:9: note: in instantiation of template class 'std::__1::__invokable_r<void, void (&)(void *) noexcept, nullptr_t &&>' requested here
__invokable<_Fp, _Args...>::value,
^
/opt/wandbox/clang-6.0.0/include/c++/v1/type_traits:4568:14: note: in instantiation of template class 'std::__1::__invoke_of<void (&)(void *) noexcept, nullptr_t &&>' requested here
: public __invoke_of<_Fp, _Args...>
^
/opt/wandbox/clang-6.0.0/include/c++/v1/type_traits:4573:22: note: in instantiation of template class 'std::__1::result_of<void (&(nullptr_t &&))(void *) noexcept>' requested here
template <class _Tp> using result_of_t = typename result_of<_Tp>::type;
^
/opt/wandbox/clang-6.0.0/include/c++/v1/functional:2349:1: note: in instantiation of template type alias 'result_of_t' requested here
result_of_t<_Fn&&(_Args&&...)>
^
prog.cc:9:3: note: while substituting deduced template arguments into function template 'invoke' [with _Fn = void (&)(void *) noexcept, _Args = <nullptr_t>]
std::invoke(std::forward<F>(f), std::forward<T>(v));
^
prog.cc:16:3: note: in instantiation of function template specialization 'for_each<void (&)(void *) noexcept, nullptr_t>' requested here
for_each(func, nullptr);
^
1 error generated.
删除func()
上的noexcept
说明符
void func(void*) /* noexcept */ {}
然后编译。我不明白这个。这是编译器错误吗?
libc++ 无法处理noexcept
标记的函数。当函数被noexcept
并且它采用错误的部分专用化(对象而不是函数时,它的机器似乎由于某种原因而失败。
由于你不能接受函数的sizeof
,所以clang正确地抱怨(gcc也会(。
解决方法是传入函数指针:
for_each(&func, nullptr);
我填写了一个错误报告,该报告已修复! :)
相关文章:
- 奇怪的结构&GCC&clang(void*返回类型)
- 数据成员SFINAE的C++17测试:gcc vs clang
- 在clang++预处理器中确定gcc工具链版本
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 如何使用Clang/GCC在Mac上为C/C++设置VSCode
- 一位朋友将模板函数缩写为clang和gcc
- 类模板参数推导-clang和gcc不同
- GCC和Clang在与__builtin_constant_p相关的static_assert方面有所不同
- 为什么 gcc 和 clang 为函数模板的实例化生成不同的符号名称?
- MSVC发现这种方法调用模棱两可,而Clang / GCC则不然吗?
- Clang&GCC以不同的方式诠释系列演员表
- MSVC:无法识别的模板声明/定义(使用 Clang/GCC 编译)
- 构造函数上的SFINAE在VC2017中工作,但在clang / gcc中不起作用
- clang/GCC编译器内在的无相应编译器标志
- Clang & GCC 误推模板参数
- Clang & GCC 在使用智能指针时不会警告非虚拟基析构函数的多态性?
- Clang/GCC插件,用于解释自定义的c++11属性
- sscanf中uint16_t的正确且可移植的(Clang/GCC)修饰符是什么
- 依赖模板可以在Visual Studio中编译,但在clang/gcc中失败