5

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?

12
  • 6
    Even ignoring constexpr, every member function defined in class body is implicitly inline. Commented Jan 7, 2022 at 13:10
  • 1
    Manually defining an empty/default destructor is almost always a mistake, unless you also define the copy/move operations. It silently replaces copying your class with moving, e.g.: gcc.godbolt.org/z/GTzdE9azs Commented Jan 7, 2022 at 13:12
  • 2
    @digito_evo "remove it" likely meant "remove the destructor (defaulted) definition". Commented Jan 7, 2022 at 13:27
  • 1
    @digito_evo When you define a function inside a class definition, it is implicitly defined as inline. ~Foo() = default; is considered a definition, so it will be inlined automagically. Commented Jan 7, 2022 at 13:36
  • 2
    Yes, sorry, I meant remove the defaulted destructor. But regarding the move operations, if they only move m_Matrix, then they should be =defaulted, and deleted copy operations can optionally be removed, because they're deleted automatically when you have custom (or defaulted) move operations. Or, if you don't mind the class being copyable, you can remove remove both copy and move operations. Commented Jan 7, 2022 at 13:37

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.