Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what c++ inline explicit constructor is good for? [duplicate]

Tags:

c++

inline

i see sometimes this constructor writing with inline explicit. for example:

protected : 
    inline explicit Singleton() { 

        CCASSERT(Singleton::instance_ == 0, "error Singleton::instance_ == 0."); 
        Singleton::instance_ = static_cast<T*>(this); 
    }
    inline ~Singleton() { 
        Singleton::instance_ = 0; 
    }

for what inline explicit is good for ?

like image 584
user63898 Avatar asked Mar 20 '23 02:03

user63898


1 Answers

inline is necessary if you define a function in a header, but not in a class definition. It allows the function to be defined in multiple translation units (i.e. when including the header from multiple source files). The purpose is to allow the compiler to inline calls to the function - many compilers require the definition to be available within the translation unit in order to do that.

In this case it's pointless: functions defined within a class definition are implicitly inline.

explicit means that the constructor can't be used for implicit type conversions. Historically, it only made sense for single-argument constructors; but I think these days it can also be used to prevent brace-initialisation.

In this case, it's also pointless: default constructors aren't used for implicit conversions.

for what inline explicit is good for ?

Here, they both give useful code smells - the author values verbiage and excessive structure over clarity. This is further evidenced by the use of the Singleton anti-pattern - tread carefully in this code.

like image 194
Mike Seymour Avatar answered Mar 23 '23 10:03

Mike Seymour