Clang warning: delegating constructors are incompatible with C++98 [-Wc++98-compat] (warn_cxx98_compat_delegating_ctor)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: delegating constructors are incompatible with C++98
Type Warning
Category Semantic Issue
Internal Id warn_cxx98_compat_delegating_ctor
Active by Default No
Flags -Wc++98-compat (131 elements)
-Wc++98-compat-pedantic (217 elements)
Internal Message delegating constructors are incompatible with C++98
Regular Expression (?:warning|error|fatal error)\: delegating constructors are incompatible with C\+\+98 \[(?:\-Werror,)?\-Wc\+\+98\-compat[^\]]*\]
First Commit 2011-10-18 0bf8a492fd75 -Wc++98-compat and -Wc++98-compat-pedantic warnings for Sema, part 1.

Description

The warning is issued by the Clang compiler when a delegating constructor is used in code that is compiled with the C++98 standard compatibility flag. Delegating constructors, a feature introduced in C++11, allow a constructor of a class to call another constructor of the same class in its initializer list to reuse code. In C++98, this feature is not available, and attempting to use it while targeting C++98 compatibility will trigger this warning. The main purpose of this warning is to alert the developer that their code employs modern C++ features not compatible with the C++98 standard, potentially leading to portability issues if the code is meant to be compatible with older C++ compilers or standards.  
AI Generated

Example

In the following example, a class named C is defined, which includes two constructors. The first constructor accepts an integer argument and uses a delegating constructor by calling another constructor of the same class in its initializer list. This called constructor is the default constructor of the class. The purpose of this delegation is to enable the reuse of the initialization code specified in the default constructor. The default constructor, when invoked, outputs a message to indicate that it has been called. In the main function, an instance of the class C is created with an integer argument. This instantiation triggers the delegation from the integer argument constructor to the default constructor. The compiler output section demonstrates the generated warning, indicating the use of delegating constructors. This feature is not compatible with the C++98 standard, hence the warning when the code is compiled with C++98 compatibility flags.  
AI Generated


Flags -xc++ -std=c++11 -Wc++98-compat

[Try out in Compiler Explorer]

Source
#include <iostream>

class C {
public:
    C(int x) : C() { // Delegating constructor
        std::cout << "Constructor with int argument: " << x << std::endl;
    }

    C() {
        std::cout << "Default constructor" << std::endl;
    }
};

int main() {
    C o(10);
    return 0;
}
Compiler Output
<source>:5:16: warning: delegating constructors are incompatible with C++98 [-Wc++98-compat]


Clang Internals (17.0.6)

Git Commit Message

-Wc++98-compat and -Wc++98-compat-pedantic warnings for Sema, part 1.

llvm-svn: 142419

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/SemaDeclCXX.cpp (line 4621)

MemInitResult Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, CXXRecordDecl *ClassDecl) {
  // ...
  Diag(NameLoc, diag::warn_cxx98_compat_delegating_ctor);

Triggered in Clang Tests

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

clang/test/SemaCXX/cxx98-compat.cpp

  • clang/test/SemaCXX/cxx98-compat.cpp:170:20: warning: delegating constructors are incompatible with C++98 [-Wc++98-compat]