Clang error: definition of explicitly defaulted ... (err_definition_of_explicitly_defaulted_member)

From emmtrix Wiki
Jump to navigation Jump to search
Text
error: definition of explicitly defaulted
default constructor
copy constructor
move constructor
copy assignment operator
move assignment operator
destructor
function

Type Error
Category Semantic Issue
Internal Id err_definition_of_explicitly_defaulted_member
Internal Message definition of explicitly defaulted %select{default constructor|copy constructor|move constructor|copy assignment operator|move assignment operator|destructor|function}0
Regular Expression (?:error|fatal error)\: definition of explicitly defaulted (?:default constructor|copy constructor|move constructor|copy assignment operator|move assignment operator|destructor|function)
First Commit 2011-05-10 6d5b96c6b3ef Further implement defaulting constructors.

Description

The error is issued by the Clang compiler when an explicitly defaulted function is redefined. In C++, constructors, destructors, copy assignment operators, and move assignment operators can be explicitly defaulted using the = default syntax in their declarations. This instructs the compiler to generate the default implementation for these functions. However, once a function has been explicitly defaulted, it must not be defined again by the programmer. This error is triggered if the compiler encounters a definition for a function that has already been explicitly defaulted, indicating a contradiction in the code where the programmer both requests the compiler to generate a default implementation and provides a manual implementation. The functions that fall under this category include the default constructor, copy constructor, move constructor, copy assignment operator, move assignment operator, and destructor.  
AI Generated

Example

In the following example, a class named C is defined with a public section that includes a default constructor declaration and an explicitly defaulted destructor. The destructor is marked with = default in its declaration within the class, indicating that it should use the compiler-generated default implementation. However, a definition for the same destructor is also provided outside the class definition body. This leads to an error because the destructor was already specified to use the default implementation with = default, making any further definition unnecessary and incorrect. The Clang compiler identifies this contradiction and issues an error message indicating that the explicitly defaulted destructor cannot be redefined.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
class C {
  public:
    C();
    ~C() = default; // Explicitly defaulted
};
C::~C() {} // Error: cannot redefine
Compiler Output
<source>:6:4: error: definition of explicitly defaulted destructor


Clang Internals (17.0.6)

Git Commit Message

Further implement defaulting constructors.

Focus is on default constructors for the time being. Currently the
exception specification and prototype are processed correctly. Codegen
might work but in all likelihood doesn't.

Note that due to an error, out-of-line defaulting of member functions is
currently impossible. It will continue to that until I muster up the
courage to admit that I secretly pray to epimetheus and that I need to
rework the way default gets from Parse -> Sema.

llvm-svn: 131115

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 4030)

/// MergeFunctionDecl - We just parsed a function 'New' from
/// declarator D which has the same name and scope as a previous
/// declaration 'Old'.  Figure out how to resolve this situation,
/// merging decls or emitting diagnostics as appropriate.
///
/// In C++, New and Old must be declarations that are not
/// overloaded. Use IsOverload to determine whether New and Old are
/// overloaded, and to select the Old declaration that New should be
/// merged with.
///
/// Returns true if there was an error, false otherwise.
bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, Scope *S, bool MergeTypeWithOld, bool NewDeclIsDefn) {
  // ...
  if (getLangOpts().CPlusPlus) {
    // ...
    if (OldMethod && NewMethod) {
      // ...
      if (!isFriend && NewMethod->getLexicalDeclContext()->isRecord() && !IsClassScopeExplicitSpecialization) {
      // ...
      } else if (OldMethod->isImplicit()) {
      // ...
      } else if (OldMethod->getFirstDecl()->isExplicitlyDefaulted() && !isFriend) {
        Diag(NewMethod->getLocation(), diag::err_definition_of_explicitly_defaulted_member) << getSpecialMember(OldMethod);

Triggered in Clang Tests

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

clang/test/SemaCXX/cxx0x-defaulted-functions.cpp

  • clang/test/SemaCXX/cxx0x-defaulted-functions.cpp:146:6: error: definition of explicitly defaulted default constructor
  • clang/test/SemaCXX/cxx0x-defaulted-functions.cpp:147:6: error: definition of explicitly defaulted copy constructor
  • clang/test/SemaCXX/cxx0x-defaulted-functions.cpp:148:6: error: definition of explicitly defaulted move constructor
  • clang/test/SemaCXX/cxx0x-defaulted-functions.cpp:149:9: error: definition of explicitly defaulted copy assignment operator
  • clang/test/SemaCXX/cxx0x-defaulted-functions.cpp:150:9: error: definition of explicitly defaulted move assignment operator
  • clang/test/SemaCXX/cxx0x-defaulted-functions.cpp:151:6: error: definition of explicitly defaulted destructor
  • clang/test/SemaCXX/cxx0x-defaulted-functions.cpp:244:10: error: definition of explicitly defaulted function