Skip to content

[c++17] lambda capture by reference does not work #233

Open
@i-garrison

Description

@i-garrison

Just realized the test below is part of https://bugs.eclipse.org/bugs/show_bug.cgi?id=561128 discussion.

The following code is at least c++17 because it evaluates constexpr lambda in constexpr context.
Computed result value is integer 2.

Eclipse CDT supports constexpr lambda and is able to perform evaluation thereof.

Unfortunately CDT evaluations of both local lambdas effectively capture acc by value so calling any of them do not affect the acc in enclosing function scope. This causes CDT to evaluate lambdainc() as integer 0 which yields wrong non-type argument to Evaluate<> .

template<int V> struct Evaluate {};
template<> struct Evaluate<2> { static constexpr int value = 2; };

constexpr auto lambdainc()
{
	int acc = 0;

	auto incDefaultCapture = [&]() constexpr { return ++acc; };
	auto incCaptureByReference = [&acc]() constexpr { return ++acc; };
	incDefaultCapture();
	incCaptureByReference();

	return acc;
}

// Error mark on 'result' initializer below says: Symbol 'value' could not be resolved
// this is because lamdainc() is currently evaluated to 0 and Evaluate<0> does not contain 'value'
static constexpr auto result = Evaluate<lambdainc()>::value;

Expected behavior
CDT evaluation of both lamdas should capture acc from enclosing function scope by reference.
lambdainc() should evaluate as integer 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    languageC/C++ Language Support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions