5

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 ?

user63898
  • 29,839
  • 85
  • 272
  • 514
  • 1
    `inline` and `explicit` are two different keywords. So I guess you're asking what `explicit` means, which has already been answered [here](https://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean). – Zeta Mar 24 '14 at 13:36
  • Are you asking about the combination between `inline` and `explicit`? Because that doesn't add anything to the respective behaviors of these two modifiers. – Frédéric Hamidi Mar 24 '14 at 13:36
  • @Zeta But what sense makes `explicit` without any constructor parameter?? – πάντα ῥεῖ Mar 24 '14 at 13:37
  • 1
    explicit on a no-arg constructor makes sense in c++11 to circumvent brace initialisation. – Bathsheba Mar 24 '14 at 13:38
  • btw, as far as i know, if you put the body of the function in the { } right in the class, the function is inlined by default – Exceptyon Mar 24 '14 at 13:38
  • @πάνταῥεῖ, see [Purpose of Explicit Default Constructors](http://stackoverflow.com/q/2836939/464709). – Frédéric Hamidi Mar 24 '14 at 13:38
  • @FrédéricHamidi THX, good to know. The answer there says, `explicit` is redundant for the OP's case. – πάντα ῥεῖ Mar 24 '14 at 14:15

2 Answers2

11

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • +1: But I would state that within a class definition, `inline` is implicit for all functions defined therein. – Sebastian Mach Mar 24 '14 at 13:47
  • 1
    @phresnel: Indeed, which is why I stated it in the second paragraph. – Mike Seymour Mar 24 '14 at 13:48
  • @AlecTeal: It's not a tautology - its purpose is to allow multiple definitions (and hence allow inlining in multiple translation units). Without it, as you say, you're not allowed multiple definitions. – Mike Seymour Mar 24 '14 at 13:54
  • Concerning brace initialization, the explicit constructor should make copy initialization, `T t = {};`, illegal, but `T t{};` should be OK. Although GCC 4.8.2 seems to allow the former. I think that is a bug. – juanchopanza Mar 24 '14 at 13:59
-3

explicit with constructors stops them being used implicitly.

inline is a way to tell compilers (in ye-olden-days when we lacked RAM) that the function is something it'd want to inline. Of course, nowadays they ignore us, or at most humour us with "I'll take it under advisement" with the subtext of "Silly human, thinks I didn't know that already?" (in short, inline is ignored these days, the compiler is in a much better position than us to decide to inline).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alec Teal
  • 5,770
  • 3
  • 23
  • 50