noexcept说明符神秘地打破了编译(clang,gcc不同意)

Lin*_*gxi 17 c++ gcc clang noexcept c++11

有问题的代码是

#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);
}
Run Code Online (Sandbox Code Playgroud)

它在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.
Run Code Online (Sandbox Code Playgroud)

删除说明noexceptfunc()

void func(void*) /* noexcept */ {}
Run Code Online (Sandbox Code Playgroud)

然后它编译.我不懂.这是编译器错误吗?

Rak*_*111 19

好吧没有.libc ++无法处理noexcept标记的函数.看起来它的机器由于某种原因在函数出现时失败noexcept并且它采用了错误的部分特化(对象不是用于函数).

因为你无法接受sizeof一个功能,所以clang正确地抱怨(并且gcc也会这样).

作为一种解决方法,传入一个函数指针:

for_each(&func, nullptr);
Run Code Online (Sandbox Code Playgroud)

我填写了一份错误报告,该报告已修复!:)

  • 确实.libc ++的`<type_traits>`对所有类型的函数类型都有`__check_complete`的特化,但是错过了`noexcept`.这可能值得向bug追踪者报告...... (6认同)