在C ++ 17中具有noexcept的std :: function [英] std::function with noexcept in C++17

查看:115
本文介绍了在C ++ 17中具有noexcept的std :: function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 17中noexcept 已添加到类型系统:

In 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.

The latest versions of GCC and Clang in C++17 mode reject the call r1(foo), because void (*)() cannot be implicitly converted to 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>的奇怪错误.

Clang accepts the program, apparently ignoring the noexcept specifier; and g++ gives a strange error regarding std::function<void() noexcept>.

在C ++ 17中第二个程序的正确行为是什么?

What is the correct behaviour for this second program in C++17?

推荐答案

std::function的定义在当前工作草案中未更改:

std::function's definition hasn't changed in the current working draft:

template<class T>
class function; // not defined

template<class R, class... ArgTypes>
class function<R(ArgTypes...)> {
    /* ... */
};

由于void() noexcept与部分专业名称不匹配,因此std::function<void() noexcept>是不完整的类型. Clang和GCC主干均会对此进行诊断.

Since void() noexcept doesn't match the partial specialization, std::function<void() noexcept> is an incomplete type. Both Clang and GCC trunk diagnose this accordingly.

这篇关于在C ++ 17中具有noexcept的std :: function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆