How many instances of static data declared inside a static member function in the header file?

I am working on compiling Cppcheck on AIX with xlC. Each verification class is derived from a class Checkwhose constructor is responsible for registering this type of verification in the global static list.

Here's the relevant part of the code in question (file names link to the 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;
}

, _instances ​​ static ( check.cpp). g++ , , instances() , , _instances. , .cpp, _instances .

AIX xlC instances() .cpp, , _instances . , _instances, Cppcheck .

?

. , , . , .

+1
3

g++ : . ++ (++ 03 7.1.2/4):

.

, - , ++ 03 3.5/5:

-... , .

- , , ++ 03 7.1.2/3:

, , .

+9

g++ (, - instances() , , ?). , instances() cpp, xlc.

+2

:

   static std::list<Check *>& instances();

// check.cpp
 std::list<Check *> & Check::instances()
 {
     static std::list<Check *> _instances;
     return _instances;
 }

, . - .

, , , , . singleton , , ODR , , , , , .

boost:: once.

+1

Source: https://habr.com/ru/post/1789361/


All Articles