c++ - 在 C++17 中使用 noexcept 的 std::function

标签 c++ function c++17 noexcept

在 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/

相关文章:

c++ - 具有不同参数顺序的聚合初始化

c++ - 为什么选择转换运算符的这种重载?

c++ - OpenGL ES 绘图形状

c++ - 在 g++ 中更改默认的 C++ 标准

C++ 两个阶乘之和等于 10 的阶乘找到两个值 x 和 y,其阶乘之和

python - python 中 __init__ 之外的 libSBML 段错误

r - 如何构建一个循环数据帧并转换其中数据的函数 (R)

javascript - 在 React 中返回并渲染一个 div

VB.NET、类和模块与函数

c++ - 如何声明一个函数接受任意长度的右值数组