16

In the C++ standard, closure types are defined as follows:

[expr.prim.lambda.closure] The type of a lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type, called the closure type, whose properties are described below. [...]

The standard does not seem to define whether or not the unnamed non-union class type is final. Would a compiler implementing lambdas as final classes be standard compliant, or do we have the guarantee that one can inherit from lambdas?

The question is not whether it is useful or not to inherit from lambdas: it's a given that it is useful. The question is whether the standard provides this guarantee.

9
  • 2
    @Matteo See std::visit, where it is cleaner to chain lambdas together (maybe even with other function objects) instead of a generic lambda with an if chain to do type specific things, in my opinion. See also P0051.
    – Rakete1111
    Apr 7, 2018 at 7:50
  • 1
    We can know it's meant to be allowed to inherit lambdas from the fact that text in the standard has been rewritten to solve problems when inheriting lambdas, that it was considered to just reject inheriting lambdas, but that it continued to be allowed after a serious use case was presented: CWG issue 751. But this doesn't answer whether it's actually allowed.
    – user743382
    Apr 7, 2018 at 8:04
  • 6
    An unnamed class can't be final :)
    – cpplearner
    Apr 7, 2018 at 8:25
  • 2
    @Rakete1111 It does mean something, because the effect of final is associated with the syntactic form. :)
    – cpplearner
    Apr 7, 2018 at 8:58
  • 2
    @Vincent It's the grammar that doesn't allow to have a final unnamed class due to the fact that final is not a keyword. class final is a class named final.
    – Rakete1111
    Apr 7, 2018 at 8:58

2 Answers 2

18

Yes, the closure type must not be final. At least that's my interpretation.

§8.1.5.1 Closure types [expr.prim.lambda.closure]

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:

  • ... [does not apply]

The standard then doesn't describe the closure type as being final. Making it final would alter the observable behavior ergo the closure type must not be final.

Regarding the observable behavior. Consider this:

auto l = []{};
return std::is_final_v<decltype(l)>;

Making the closure type final would clearly modify the observable behavior of a valid program.


As to a use case, it can actually be a very useful feature:

template <class... Fs> struct Overload : Fs ...
{
    using Fs::operator()...;
};

template <class... Fs> Overload(Fs...) -> Overload<Fs...>;

auto test()
{
    Overload f = {[] (int a) { return a * 100; },
                  [] (int a, int b) { return a + b;}};

    return f(1) + f(2, 3); // 105
}

See it in action on godbolt


Thanks to hvd and rakete1111 for the discussions and feedback in the comments.

9
  • 1
    The logic would work the other way around: if the standard doesn't prohibit implementations from making the generated type final, implementations are free to do so. Luckily, the standard does appear to prohibit implementations from doing so: [expr.prim.lambda]p3 or [expr.prim.lambda.closure]p2. But I'm not entirely sure about it.
    – user743382
    Apr 7, 2018 at 7:34
  • 2
    @hvd Could you cite where the standard prohibit closure types from being final?
    – Vincent
    Apr 7, 2018 at 7:39
  • 2
    @Vincent The "[...] provided this does not alter the observable behavior of the program other than by changing [...]" bit: making the closure type final would alter the observable behaviour. The thing I'm not entirely sure about is whether unspecified aspects in there and the following paragraphs are meant to be covered by that.
    – user743382
    Apr 7, 2018 at 7:43
  • 1
    @hvd Wrong quotes. Observable behavior can only happen if the program is well-formed. [expr.prim.lambda]p2 doesn't apply because final is not a decl-specifier, and [expr.prim.lambda.closure]p2 is basically lessening the requirements of the as-if rule for lambdas.
    – Rakete1111
    Apr 7, 2018 at 8:09
  • 1
    @Rakete1111 1) I don't subscribe to your logic that there is no difference in observable behaviour between an accepted program and a rejected program. 2) Consider a program which prints out std::is_final_v<T> if it makes you feel better. 3) The text moved, that's why I provided two alternative locations where the text may be found. 4) That's one interpretation, and why I'm not sure about it.
    – user743382
    Apr 7, 2018 at 9:39
0

The effect of final is specified in [class]/3:

If a class is marked with the class-virt-specifier final and it appears as a class-or-decltype in a base-clause, the program is ill-formed.

That is, it doesn't matter whether the class is final. It only matters whether the class is marked with the final specifier. Since a closure type does not have any declaration in the source file, it is impossible to mark it as final, and thus [class]/3 does not apply.

8
  • 7
    This is a completely ludicrous interpretation. By the same logic, lambdas aren't callable, because "A function declaration having one of the following operator-function-ids as its name declares an operator function." doesn't apply to the operator() provided by the lambda, as it is not introduced by the operator-function-ids syntax construct.
    – user743382
    Apr 7, 2018 at 9:50
  • 1
    "Operator function" is in italics. Italics can mean different things depending on context, but in this context, it means it's the definition of the term (see [intro.defs]p3), so it means there is no such thing as an operator function other than a function declared by [see quote].
    – user743382
    Apr 7, 2018 at 10:59
  • 1
    @hvd From my knowledge of English, the language does not work in that way.
    – cpplearner
    Apr 7, 2018 at 11:11
  • 3
    Did you read [intro.defs]p3? It doesn't matter whether that's how italics work in ordinary English, it matters that that's how italics work in the C++ standard.
    – user743382
    Apr 7, 2018 at 11:13
  • 1
    @PasserBy Makes sense, but ... github.com/cplusplus/draft/issues/1820#issuecomment-345398277
    – cpplearner
    Apr 10, 2018 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.