可以C ++ lambda表达式抛出? [英] Can C++ lambda-expression throw?

查看:123
本文介绍了可以C ++ lambda表达式抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构造闭包对象期间,我找不到关于抛出异常的任何东西。

I can't find anything about throwing exceptions during constructing closure object.

在向量的复制构建过程中,这个表达式可能会抛出:

It's oblivious that this expression can throw during copy construction of the vector:

auto v = std::vector<int>(1000000);
[v]{};

但是,空白或通过引用捕获列表如下:

But what about empty or "by reference" capture lists like this:

[&]{};

我现在只谈关闭对象的构造。调用不是有趣的。

I'm speaking only about construction of closure object now. Calling isn't interesting.

我读了 5.1.2 Lambda表达式[expr.prim.lambda] ,但是

推荐答案

根据标准( draft n3337 ),§5.1.2/ 3:

According to the standard (draft n3337), §5.1.2/3:


lambda表达式的类型(也是闭包对象的类型)是一个唯一的,未命名的不兼容的
类类型 - 称为闭包类型,其属性如下所述。此类类型不是
一个聚合(8.5.1)。闭包类型在包含相应的lambda表达式的最小块范围,类范围或命名空间
范围内声明。

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed nonunion class type — called the closure type — whose properties are described below. This class type is not an aggregate (8.5.1). The closure type is declared in the smallest block scope, class scope, or namespace scope that contains the corresponding lambda-expression.

简而言之,lambda表达式最终实例化一个匿名类型(仅对编译器知道),包含存储捕获列表中指示的值的成员变量。所以想象你看到一个看起来像这样的课程:

In a nutshell, a lambda expression ultimately instantiates an anonymous type (known only to the compiler), containing member variables that store the values indicated in the capture list. So imagine you saw a class that looked like this:

class __IgnoreMe__
{
    std::vector<int>& _v;

public:
    __IgnoreMe__(std::vector<int>& v)
        : _v(v)
    {
    }

    void operator()()
    {
    }
};

注意:编译器看起来,这个标准规定了生成的类的特定要求,为了简洁起见,我已经省略了这些要求。)

(Note: this isn't exactly how the class created by the compiler looks. The standard lays out specific requirements for generated classes, which I've left out for brevity's sake.)

现在想象一下你实例化了这个类: p>

Now imagine you instantiate that class like this:

auto v = std::vector<int>(1000000);
auto my_lambda = __IgnoreMe__(v);

可以实例化 my_lambda 抛出异常?如果是这样,那么关闭对象的构造就会抛出异常。 (在这种情况下不可以)。

Can the instantiation of my_lambda throw an exception? If so, then the construction of the closure object can throw an exception. (It can't, in this case.)

对于提供nothrow保证,标准不要求编译器说明这一点,但它也不会阻止他们这样做。 §5.1.2/ 3的结尾指出:

As for providing nothrow guarantees, the standard doesn't require compilers to account for this, but it doesn't prevent them from doing it either. The end of §5.1.2/3 states:


一个实现可以定义关闭
类型与下面描述的不同只要这不会改变
程序的可观察行为,而不是通过更改:

An implementation may define the closure type differently from what is described below provided this does not alter the observable behavior of the program other than by changing:

- 闭包类型的大小和/或对齐方式,

— the size and/or alignment of the closure type,

- 封闭类型是否可以复制(条款9),

— whether the closure type is trivially copyable (Clause 9),

- 封闭类型是否为标准布局类(第9条),或

— whether the closure type is a standard-layout class (Clause 9), or

- 封闭类型是否为POD类(第9条)。

— whether the closure type is a POD class (Clause 9).

这篇关于可以C ++ lambda表达式抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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