Clang warning: explicit(bool) is a C++20 extension [-Wc++20-extensions] (ext_explicit_bool)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: explicit(bool) is a C++20 extension (since 10.0)
Type Warning
Category Parse Issue (since 10.0)
Internal Id ext_explicit_bool (since 10.0)
Active by Default Yes
Flags -Wno-c++20-extensions (23 elements)
-Wno-c++2a-extensions (23 elements)
Internal Message explicit(bool) is a C++20 extension (since 10.0)
Regular Expression (?:warning|error|fatal error)\: explicit\(bool\) is a C\+\+20 extension \[(?:\-Werror,)?\-Wc\+\+20\-extensions[^\]]*\]
First Commit 2019-10-09 739b410f1ff5 Add a warning, flags and pragmas to limit the number of pre-processor tokens in a translation unit

Description

The warning is issued by the Clang compiler when an explicit specifier is used with a boolean argument, a feature that is introduced in C++20. In versions of C++ prior to C++20, the explicit specifier is only used to declare constructors that cannot be used for implicit conversions. Starting with C++20, the explicit specifier can be used with a boolean argument to indicate whether a constructor or conversion operator is explicit, providing more fine-grained control over conversions. When code that is compiled with standards set before C++20 uses the explicit(bool) syntax, Clang generates this warning to inform the developer that they are using a feature not available in the selected C++ standard version. This helps in maintaining compatibility with earlier C++ standards and aids in transition planning for developers moving to C++20.  
AI Generated

Example

In the following example, a structure named S is defined with a constructor that takes an integer argument. This constructor uses the explicit specifier with a boolean argument, false, a feature introduced in C++20 to control convertibility. The code is meant to be compiled under the C++17 standard, as indicated by the compiler options. However, the use of explicit(false) is not valid in C++17, leading to a warning from the Clang compiler. This warning indicates that the feature is a C++20 extension and is not available in the selected C++ standard version being used, which in this case is C++17. The compiler output shows the warning message generated by Clang, underscoring the misuse of a C++20 feature in a codebase targeting an earlier standard version.  
AI Generated


Flags -xc++ -std=c++17 -Wc++20-extensions

[Try out in Compiler Explorer]

Source
// C++17 code
struct S {
  // C++20 feature used
  explicit(false) S(int) {}
};
Compiler Output
<source>:4:11: warning: explicit(bool) is a C++20 extension [-Wc++20-extensions]


Clang Internals (17.0.6)

Git Commit Message

Add a warning, flags and pragmas to limit the number of pre-processor tokens in a translation unit

See
https://docs.google.com/document/d/1xMkTZMKx9llnMPgso0jrx3ankI4cv60xeZ0y4ksf4wc/preview
for background discussion.

This adds a warning, flags and pragmas to limit the number of
pre-processor tokens either at a certain point in a translation unit, or
overall.

The idea is that this would allow projects to limit the size of certain
widely included headers, or for translation units overall, as a way to
insert backstops for header bloat and prevent compile-time regressions.

Differential revision: https://reviews.llvm.org/D72703

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/Parse/ParseDecl.cpp (line 4047)

/// ParseDeclarationSpecifiers
///       declaration-specifiers: [C99 6.7]
///         storage-class-specifier declaration-specifiers[opt]
///         type-specifier declaration-specifiers[opt]
/// [C99]   function-specifier declaration-specifiers[opt]
/// [C11]   alignment-specifier declaration-specifiers[opt]
/// [GNU]   attributes declaration-specifiers[opt]
/// [Clang] '__module_private__' declaration-specifiers[opt]
/// [ObjC1] '__kindof' declaration-specifiers[opt]
///
///       storage-class-specifier: [C99 6.7.1]
///         'typedef'
///         'extern'
///         'static'
///         'auto'
///         'register'
/// [C++]   'mutable'
/// [C++11] 'thread_local'
/// [C11]   '_Thread_local'
/// [GNU]   '__thread'
///       function-specifier: [C99 6.7.4]
/// [C99]   'inline'
/// [C++]   'virtual'
/// [C++]   'explicit'
/// [OpenCL] '__kernel'
///       'friend': [C++ dcl.friend]
///       'constexpr': [C++0x dcl.constexpr]
void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, const ParsedTemplateInfo &TemplateInfo, AccessSpecifier AS, DeclSpecContext DSContext, LateParsedAttrList *LateAttrs, ImplicitTypenameContext AllowImplicitTypename) {
  // ...
  while (true) {
    // ...
    case tok::kw_explicit: {
      // ...
      if (Tok.is(tok::l_paren)) {
        if (getLangOpts().CPlusPlus20 || isExplicitBool() == TPResult::True) {
          Diag(Tok.getLocation(), getLangOpts().CPlusPlus20 ? diag::warn_cxx17_compat_explicit_bool : diag::ext_explicit_bool);

Triggered in Clang Tests

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

clang/test/Parser/explicit-bool.cpp

  • clang/test/Parser/explicit-bool.cpp:47:12: warning: explicit(bool) is a C++20 extension [-Wc++20-extensions]
  • clang/test/Parser/explicit-bool.cpp:52:12: warning: explicit(bool) is a C++20 extension [-Wc++20-extensions]