Skip to content

Confusing error messages about non-literal types being used in constant expressions #60311

Closed
@tbaederr

Description

@tbaederr

Consider this example code:

class Base {
public:
  virtual ~Base() {}
};
constexpr int test() {
  Base b;
  return 0;
}

clang -std=c++20 complains:

<source>:8:8: error: variable of non-literal type 'Base' cannot be defined in a constexpr function before C++2b
  Base b;
       ^
<source>:3:11: note: 'Base' is not literal because its destructor is not constexpr
  virtual ~Base() {}
          ^
1 error generated.

However, using -std=c++2b instead gives:

<source>:7:15: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr int test() {
              ^
<source>:8:8: note: non-literal type 'Base' cannot be used in a constant expression
  Base b;
       ^
1 error generated.

See: https://godbolt.org/z/aEbYKxfjT

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++20clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerconstexprAnything related to constant evaluation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions