[cfe-dev] Lambda capture of constexpr variables
Jackie Kay via cfe-dev
cfe-dev at lists.llvm.org
Sun Jun 11 04:16:35 PDT 2017
In C++17, is a constexpr-declared variable is captured by copy in a lambda
a core constant expression within the body of the lambda?
For example, GCC fails to compile the following example because it
considers "i" not to be a constant expression within the body of the
lambda, while Clang accepts it. Removing the capture of i reverses the
behavior (Clang rejects it and GCC accepts it).
#include <utility>
template<int I>
constexpr auto unwrap(std::integral_constant<int, I>) {
return I;
}
int main() {
constexpr auto i = std::integral_constant<int, 42>{};
constexpr auto l = [i]() {
constexpr int x = unwrap(i);
};
}
GCC Wandbox link: https://wandbox.org/permlink/oB7VEymVTfDQuxYG
Clang Wandbox link: https://wandbox.org/permlink/2H7zezNFXdQdyaBz
On Stack Overflow
<https://stackoverflow.com/questions/44386415/gcc-and-clang-disagree-about-c17-constexpr-lambda-captures>,
it was suggested that this is a bug in Clang and the closure type should
not be a constant expression, but I wanted to get a second opinion from the
Clang developers.
I'm invested in this question because I recently wrote a C++17 library that
makes heavy use of Clang's behavior in this case, but using this idiom
makes it difficult to support GCC and Clang without a lot of ifdefs. So I'd
like to confirm the correct interpretation of the standard before I decide
on how to refactor my code.
Thanks for reading,
Jackie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170611/c5030186/attachment.html>
More information about the cfe-dev
mailing list