Clang error: cannot decompose ... member B of D (err_decomp_decl_inaccessible_field)

From emmtrix Wiki
Jump to navigation Jump to search
Text
error: cannot decompose
private
protected
member B of D

(since 8.0)

Type Error
Category Semantic Issue (since 8.0)
Internal Id err_decomp_decl_inaccessible_field (since 8.0)
Internal Message cannot decompose %select{private|protected}0 member %1 of %3 (since 8.0)
Regular Expression (?:error|fatal error)\: cannot decompose (?:private|protected) member (.*?) of (.*?)
First Commit 2018-09-26 5c9b3b757623 P0969R0: allow structured binding of accessible members, not only public members.

Description

The error is issued by the Clang compiler when it encounters an attempt to decompose a class object into its members using structured bindings, and one or more of these members are either private or protected. Structured bindings, introduced in C++17, allow for the decomposition of return values or objects into individual variables. However, for an object's members to be decomposable, they must be accessible to the context attempting the decomposition. This means that the members must be public or accessible through friend declarations when the decomposition is attempted from outside the class. If a member is private or protected and thus not accessible in the context where the decomposition is attempted, Clang will issue this error, indicating that the member cannot be decomposed due to its access level. This error usually prompts the developer to check the access specifier of the members they are trying to decompose, ensuring they are not attempting to access members that are not publicly available to the current context.  
AI Generated

Example

In the following example, a C++ class named C is defined with a private member variable x. The main function attempts to use structured bindings to decompose an object of type C into its constituent members. Structured bindings are a feature introduced in C++17 that allows for the decomposition of an object's members into individual variables. However, because the member x is private, it cannot be directly accessed outside of class methods or from the class's friends. The attempt to decompose the object in this way violates the access control rules of C++, leading the Clang compiler to issue an error message. This error underscores the importance of ensuring that members intended for decomposition through structured bindings are accessible in the context where the decomposition is attempted.  
AI Generated


Flags -xc++ -std=c++17

[Try out in Compiler Explorer]

Source
// Class with private member
class C {
private:
  int x;
};

int main() {
  C c;
  // Attempt to decompose private member
  auto [a] = c;
}
Compiler Output
<source>:10:9: error: cannot decompose private member 'x' of 'C'
<source>:4:7: note: declared private here


Clang Internals (17.0.6)

Git Commit Message

P0969R0: allow structured binding of accessible members, not only public members.

llvm-svn: 343036

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/SemaAccess.cpp (line 1772)

/// Checks implicit access to a member in a structured binding.
Sema::AccessResult Sema::CheckStructuredBindingMemberAccess(SourceLocation UseLoc, CXXRecordDecl *DecomposedClass, DeclAccessPair Field) {
  // ...
  Entity.setDiag(diag::err_decomp_decl_inaccessible_field);

Triggered in Clang Tests

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

clang/test/CXX/dcl.decl/dcl.decomp/p4.cpp

  • clang/test/CXX/dcl.decl/dcl.decomp/p4.cpp:28:11: error: cannot decompose protected member 'a' of 'NonPublicMembers::NonPublic1'
  • clang/test/CXX/dcl.decl/dcl.decomp/p4.cpp:29:11: error: cannot decompose private member 'a' of 'NonPublicMembers::NonPublic2'
  • clang/test/CXX/dcl.decl/dcl.decomp/p4.cpp:31:11: error: cannot decompose private member 'a' of 'NonPublicMembers::NonPublic2'
  • clang/test/CXX/dcl.decl/dcl.decomp/p4.cpp:232:18: error: cannot decompose protected member 'y' of 'p0969r0::C'
  • clang/test/CXX/dcl.decl/dcl.decomp/p4.cpp:239:15: error: cannot decompose protected member 'y' of 'p0969r0::C'