c++destructorsonarlintinline-functionsconstexpr-function

A "constexpr" function should not be declared "inline"


By analyzing code using SonarLint, I got a message (the title of the question) about a destructor that is declared like below:

class Foo
{
public:
.   // default ctor
.   // parameterized ctor
.
    inline ~Foo() = default; // dtor
.
.   // copy ctor = delete
.   // copy assignment operator = delete
.   // move ctor
.   // move assignment operator

private:
    ...
    mutable std::vector< std::vector<char> > m_Matrix;
    ...
};

Here is the message's description: Declaring a function or a static member variable constexpr makes it implicitly inline.

I don't think the dtor of this class can be constexpr or consteval because it has a non-static data member of type std::vector so ~Foo has to call delete[] at some point to deallocate the vector's storage.

So why is SonarLint showing this message? Is it because of = default? Does any defaulted special member function become implicitly constexpr?


Solution

  • Yes:

    An explicitly-defaulted function that is not defined as deleted may be declared constexpr or consteval only if it is constexpr-compatible ([special], [class.compare.default]). A function explicitly defaulted on its first declaration is implicitly inline ([dcl.inline]), and is implicitly constexpr ([dcl.constexpr]) if it is constexpr-compatible.

    (From Explicitly defaulted functions, emphasis mine.)

    Foo is likely constexpr-compatible in C++20, because std::vector can now be constexpr.