Skip to content

Clang rejects referencing local variable of class type to initialize template parameter in body of function template #49927

Closed
@davidstone

Description

@davidstone
Bugzilla Link 50583
Version trunk
OS Linux
CC @DougGregor,@zygoloid

Extended Description

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluationlambdaC++11 lambda expressions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions