Skip to content

constexpr value in a lambda #26001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
llvmbot opened this issue Nov 24, 2015 · 6 comments
Open

constexpr value in a lambda #26001

llvmbot opened this issue Nov 24, 2015 · 6 comments
Labels
bugzilla Issues migrated from bugzilla c++14 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party

Comments

@llvmbot
Copy link
Member

llvmbot commented Nov 24, 2015

Bugzilla Link 25627
Version unspecified
OS Linux
Attachments as explained
Reporter LLVM Bugzilla Contributor
CC @DaemonSnake

Extended Description

If I am correct the following code should be valid c++14:

template<int j> void g() {}
int main()
{
  constexpr int i = 0;
  auto f = []{ g<i>(); };
  return 0;
}

However, clang does not compile this code.
It does compile with when capturing [=], [i] or [&].
It also compiles if the i is made static.

This is related to a stackoverflow-question:
http://stackoverflow.com/questions/33873788/how-can-i-use-a-constexpr-value-in-a-lambda

clang version:
Debian clang version 3.8.0-svn247818-1~exp1 (trunk) (based on LLVM 3.8.0)
compile command:

clang++-3.8 -std=c++14 capture_constexpr.cpp

compiler error:
capture_constexpr.cpp:11:7: error: variable 'i' cannot be implicitly captured in a lambda with no capture-default specified
    g<i>();
      ^
capture_constexpr.cpp:7:17: note: 'i' declared here
  constexpr int i = 0;
                ^
capture_constexpr.cpp:10:12: note: lambda expression begins here
  auto f = []{ // compiles with gcc 4.9.1, not with clang 3.8
           ^
1 error generated.
@llvmbot
Copy link
Member Author

llvmbot commented Apr 3, 2017

Sorry about the delayed attention - I became aware of this bug just earlier today.

Patch submitted for review : https://reviews.llvm.org/D31588

@llvmbot
Copy link
Member Author

llvmbot commented May 21, 2017

@DaemonSnake
Copy link

DaemonSnake commented Dec 9, 2019

This issue was only partially fixed.
The same code in a template context won't compile:

//works

void f0()
{
   constexpr int i = 0;
   []{ return i; };
}

//error

template<class T>
void f1()
{
   constexpr int i = 0;
   []{ return i; }; //i cannot be implicitly captured ...
}

godbolt link: https://godbolt.org/z/3Sc46U

console output:

<source>:5:4: warning: expression result unused [-Wunused-value]

   []{ return i; };

   ^~~~~~~~~~~~~~~

<source>:13:15: error: variable 'i' cannot be implicitly captured in a lambda with no capture-default specified

   []{ return i; };

              ^

<source>:12:18: note: 'i' declared here

   constexpr int i = 0;

                 ^

<source>:13:4: note: lambda expression begins here

   []{ return i; };

   ^

1 warning and 1 error generated.

Compiler returned: 1

@llvmbot
Copy link
Member Author

llvmbot commented Dec 6, 2020

A patch to address this bug is under review: https://reviews.llvm.org/D92733

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@llvmbot llvmbot added the confirmed Verified by a second party label Jan 26, 2022
@royjacobson royjacobson added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label May 5, 2022
@llvmbot
Copy link
Member Author

llvmbot commented May 5, 2022

@llvm/issue-subscribers-clang-frontend

@cor3ntin
Copy link
Contributor

No longer crashes but behavior still incorrect for the template case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++14 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party
Projects
None yet
Development

No branches or pull requests

4 participants