Clang error: cannot deduce type for lambda capture B from ... initializer list (err_init_capture_paren_braces)

From emmtrix Wiki
Jump to navigation Jump to search
Text
error: cannot deduce type for lambda capture B from
parenthesized
nested
initializer list

Type Error
Category Lambda Issue
Internal Id err_init_capture_paren_braces
Internal Message cannot deduce type for lambda capture %1 from %select{parenthesized|nested}0 initializer list
Regular Expression (?:error|fatal error)\: cannot deduce type for lambda capture (.*?) from (?:parenthesized|nested) initializer list
First Commit 2014-03-12 66204ecff985 DR1346: a parenthesized braced-init-list cannot be used as the initializer when

Description

The error is issued by the Clang compiler when attempting to deduce the type of a variable captured by a lambda expression from an initializer list. This situation arises in the context of using auto type deduction for lambda capture, specifically when the lambda capture is initialized using either a parenthesized or a nested initializer list. The compiler expects to deduce the type of captured variables automatically. However, if the initializer for the lambda capture is given in the form of a parenthesized or nested initializer list, the compilation fails because Clang cannot infer the type from such initializer lists. This limitation stems from the rules governing auto type deduction and the specific handling of initializer lists in C++. As a result, the programmer is notified through this error message that the type cannot be deduced, prompting a revision of the lambda expression's capture list or its initializer syntax to resolve the issue.  
AI Generated

Example

In the following example, a lambda expression is defined within the main function. The lambda attempts to capture a variable x by copying, using a braced initializer list ({1, 2}) to initialize x. This demonstrates an attempt to utilize auto type deduction for a lambda capture with an initializer list. However, the attempt to deduce the type of x from either a parenthesized or nested initializer list results in an error. The Clang compiler emits an error message indicating that it cannot deduce the type for the lambda capture x from the provided initializer list, which is parenthesized in this case. The example is constructed to illustrate the specific conditions under which Clang fails to deduce the type of a lambda capture initialized in such a manner, in accordance with the rules governing auto type deduction and the handling of initializer lists in C++.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
int main() {
  // Cannot deduce type for lambda capture from initializer list
  auto l = [x({1, 2})](){};
}
Compiler Output
<source>:3:14: error: cannot deduce type for lambda capture 'x' from parenthesized initializer list


Clang Internals (17.0.6)

Git Commit Message

DR1346: a parenthesized braced-init-list cannot be used as the initializer when
performing auto type deduction.

llvm-svn: 203683

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaDecl.cpp (line 12747)

QualType Sema::deduceVarTypeFromInitializer(VarDecl *VDecl, DeclarationName Name, QualType Type, TypeSourceInfo *TSI, SourceRange Range, bool DirectInit, Expr *Init) {
  // ...
  if (DirectInit && isa<InitListExpr>(DeduceInit)) {
    Diag(Init->getBeginLoc(), IsInitCapture ? diag::err_init_capture_paren_braces : diag::err_auto_var_init_paren_braces) << isa<InitListExpr>(Init) << VN << Type << Range;

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/CXX/expr/expr.prim/expr.prim.lambda/p11-1y.cpp

  • clang/test/CXX/expr/expr.prim/expr.prim.lambda/p11-1y.cpp:53:21: error: cannot deduce type for lambda capture 'a' from nested initializer list