从函数返回对对象的静态常量引用

Returning static const reference to object from functions

本文关键字:静态 常量 引用 对象 函数 返回      更新时间:2023-10-16

在有效C++(第 18 项:使接口易于正确使用和难以正确使用)中,我看到了类似于以下内容的代码示例:

class Month
{
public:
    static Month Jan()
    {
        return Month(1);
    }
    static Month Feb()
    {
        return Month(2);
    }
    //...
    static Month Dec()
    {
        return Month(12);
    }
private:
    explicit Month(int nMonth)
        : m_nMonth(nMonth)
    {
    }
private:
    int m_nMonth;
};
Date date(Month::Mar(), Day(30), Year(1995));

更改函数以使它们返回对 Month 的静态常量引用是否有任何缺点?

class Month
{
public:
    static const Month& Jan()
    {
        static Month month(1);
        return month;
    }
    static const Month& Feb()
    {
        static Month month(2);
        return month;
    }
    //...
    static const Month& Dec()
    {
        static Month month(12);
        return month;
    }
private:
    explicit Month(int nMonth)
        : m_nMonth(nMonth)
    {
    }
private:
    int m_nMonth;
};

我认为第二个版本比第一个版本更有效率。

原因1:它不是更好。

按值返回会产生复制整个对象的成本。

按引用返回会产生复制有效指针的成本,以及取消引用该指针的成本。

由于Monthint的大小:

    复制
  • 引用并不比复制Month
  • 每次访问该引用时都会发生取消引用。

因此,一般来说,通过 const 引用返回是一种优化,旨在防止代价高昂的副本。

原因2:static使情况变得更糟

与 C 不同,C++承诺函数中的静态变量将在函数第一次调用时构造。

实际上,这意味着对函数的每次调用都必须以一些看不见的逻辑开头,以确定它是否是第一次调用。

另见沃恩·卡托的回答

另请参阅ildjam的评论

某些编译器不会内联包含静态局部变量的方法,内联是这里最重要的性能优化。