本文介绍了在头文件中静态成员函数中声明的静态数据有多少个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xlC 在AIX上编译 $ c>。每个检查器类派生自检查类,其构造函数负责在全局静态列表中注册该类型的检查器。



下面是相关代码的相关部分(文件名链接到Github上的完整源代码):





  class Check {
public:
Check(){
instances()。push_back(this);
instances()。sort();
}
static std :: list< Check *> & instances(){
static std :: list< Check *> _实例;
return _instances;
}
// ...
};





  class CheckBufferOverrun:public Check {
// ...
};





  //注册此检查类通过创建它的静态实例)
命名空间
{
CheckBufferOverrun instance;
}

注意 _instances static变量声明在文件中的 static 函数中(没有相应的 check.cpp 文件)。当使用 g ++ 编译时,编译器和链接器一起工作,以确保只有一个实现的静态 instances()函数,因此只有一个静态 _instances 列表的实例。在不同的 .cpp 文件中实例化的所有不同的检查器类在同一个 _instances 列表中注册。



但是,在AIX的 xlC 下,这个代码最终创建了一个不同 instances()函数为每个 .cpp 文件中包含它,每个都有不同的静态 _instances 列表。因此,不再有一个中心 _instances 列表,这导致Cppcheck不运行大多数检查。



在这种情况下哪个编译器的行为是正确的?



更新:这个问题不是如何解决问题,我已经这么做了。

解决方案

g ++具有正确的行为:应该只是对象的一个​​实例。 C ++标准说(C ++ 03 7.1.2 / 4):

因为成员函数在类定义中定义,所以它是一个内联函数,根据C ++ 03 7.1.2 / 3:


I'm working on compiling Cppcheck on AIX using xlC. Every checker class is derived from a Check class, whose constructor is responsible for registering that type of checker in a global static list.

Here's the relevant part of the code in question (filenames link to full source on Github):

check.h

class Check {
public:
    Check() {
        instances().push_back(this);
        instances().sort();
    }
    static std::list<Check *> &instances() {
        static std::list<Check *> _instances;
        return _instances;
    }
    // ...
};

checkbufferoverrun.h

class CheckBufferOverrun: public Check {
    // ...
};

checkbufferoverrun.cpp

// Register this check class (by creating a static instance of it)
namespace
{
CheckBufferOverrun instance;
}

Notice how the _instances static variable is declared inside a static function in the header file (there is no corresponding check.cpp file). When compiled with g++, the compiler and linker work together to ensure that there is only one implementation of the static instances() function, and therefore only one instance of the static _instances list. All the different checker classes instantiated in different .cpp files get registered in the same _instances list together.

However, under AIX's xlC, this same code ends up creating a different instances() function for every .cpp file in which it is included, which each having a different static _instances list. So there is no longer a single central _instances list, which causes Cppcheck to not run most of its checks.

Which compiler's behaviour is correct in this case?

Update: This question is not about how to fix the problem, I've already done that. I'm curious about which behaviour is correct.

解决方案

g++ has the correct behavior: there should be exactly one instance of the object. The C++ Standard says (C++03 7.1.2/4):

Because the class has external linkage, the static member function also has external linkage, per C++03 3.5/5:

Because the member function is defined in the class definition, it is an inline function, per C++03 7.1.2/3:

这篇关于在头文件中静态成员函数中声明的静态数据有多少个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:39