[llvm-bugs] [Bug 50583] New: Clang rejects referencing local variable of class type to initialize template parameter in body of lambda defined in template

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Jun 4 15:07:56 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=50583

            Bug ID: 50583
           Summary: Clang rejects referencing local variable of class type
                    to initialize template parameter in body of lambda
                    defined in template
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: davidfromonline at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

The following well-formed translation unit

```
struct integral_constant {
        static constexpr auto value = 0;
};

template<int>
struct s {};

template<typename>
void function_template() {
        [] {
                constexpr auto x = integral_constant();
                s<x.value>();
        };
}

void f() {
        function_template<void>();
}
```

is rejected by clang with the error message

```
<source>:12:5: error: variable 'x' cannot be implicitly captured in a lambda
with no capture-default specified
                s<x.value>();
                  ^
<source>:17:2: note: in instantiation of function template specialization
'function_template<void>' requested here
        function_template<void>();
        ^
<source>:11:18: note: 'x' declared here
                constexpr auto x = integral_constant();
                               ^
<source>:10:2: note: lambda expression begins here
        [] {
        ^
<source>:10:3: note: capture 'x' by value
        [] {
         ^
         x
<source>:10:3: note: capture 'x' by reference
        [] {
         ^
         &x
<source>:10:3: note: default capture by value
        [] {
         ^
         =
<source>:10:3: note: default capture by reference
        [] {
         ^
         &
1 error generated.
Compiler returned: 1
```

See it live: https://godbolt.org/z/a5Mjh8ojv




A very similar translation unit,

```
struct integral_constant {
};

template<auto>
struct s {};

template<typename>
void function_template() {
        [] {
                constexpr auto x = integral_constant();
                s<x>();
        };
}

void f() {
        function_template<void>();
}
```

is rejected for the same bogus reason.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210604/c7b8e2c8/attachment-0001.html>


More information about the llvm-bugs mailing list