在 C++17 中 noexcept
has been added to the type system :
void r1( void (*f)() noexcept ) { f(); }
void foo() { throw 1; }
int main()
{
r1(foo);
}
最新版本的C++17模式的GCC和Clang拒绝调用r1(foo)
, 因为 void (*)()
不能隐式转换为 void (*)() noexcept
.
但是对于 std::function
而是:
#include <functional>
void r2( std::function<void() noexcept> f ) { f(); }
void foo() { throw 1; }
int main()
{
r2(foo);
}
Clang 接受程序,显然忽略了 noexcept
说明符;和 g++
给出一个关于 std::function<void() noexcept>
的奇怪错误.
C++17 中第二个程序的正确行为是什么?
最佳答案
std::function
的定义在当前工作草案中没有改变:
template<class T>
class function; // not defined
template<class R, class... ArgTypes>
class function<R(ArgTypes...)> {
/* ... */
};
自从 void() noexcept
与部分特化不匹配,std::function<void() noexcept>
是不完全类型。 Clang 和 GCC 主干都会相应地对此进行诊断。
关于c++ - 在 C++17 中使用 noexcept 的 std::function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41293025/