Skip to content

[cling] lambdas have unexpected semantics in the interactive interpreter #22624

Description

@silverweed

Explain what you would like to see improved and how.

Running the following lines in the interpreter (line by line, not as a macro) produces an unexpected result (as in, a result that doesn't logically follow from surface knowledge of how the interpreter treats line-by-line statements + how C++ behaves):

int foo = 1
auto f = [=] { return foo; }
f()
foo = 2
f()

Expected output:

(int) 1
(int) 1

Actual output:

ROOT_prompt_1:1:1: warning: captures will be by reference, not copy
auto f = [=]{return foo;}
^
(int) 1
(int) 2

The same happens if we use explicit capture by name: auto f = [foo=foo] { return foo; }.

This is unexpected because, even knowing the variable declarations will be hoisted to the global scope, in regular C++:

  • one cannot declare a global capture-default lambda; (should not compile)
  • one can declare a global capture-by-named-copy lambda ([foo=foo]) and it works as expected (captures the value, not the reference).

NOTE: running the same code as a macro works as expected.

ROOT version

master

Installation method

any

Operating system

Linux (likely any)

Additional context

No response

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions