“constexpr”函数不应声明为“内联”

dig*_*evo 5 c++ destructor inline-functions sonarlint constexpr-function

通过使用SonarLint分析代码,我收到了一条关于析构函数的消息(问题的标题),其声明如下:

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;
    ...
};
Run Code Online (Sandbox Code Playgroud)

以下是消息的描述声明函数或静态成员变量 constexpr 使其隐式内联。

我不认为此类的 dtor 可以是constexprorconsteval因为它具有类型的非静态数据成员std::vector,因此~Foo必须delete[]在某个时刻调用以释放向量的存储。

那么为什么SonarLint显示此消息呢?是因为吗 = default?是否有任何默认的特殊成员函数变得隐式constexpr

sig*_*gma 5

是的:

可以声明未定义为删除的显式默认函数,constexpr或者consteval仅当它与 constexpr 兼容时([special]、[class.compare.default])。 在其第一个声明中显式默认的函数是隐式内联的 ([dcl.inline]),并且如果它与 constexpr 兼容,则它是隐式 constexpr ([dcl.constexpr])。

(来自显式默认函数,强调我的。)

Foo 在 C++20 中可能与 constexpr 兼容,因为std::vector现在可以是constexpr.