what c++ inline explicit constructor is good for?

This question already has an answer here:

  • What does the explicit keyword mean? 11 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 (ie 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.


    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).

    链接地址: http://www.djcxy.com/p/24552.html

    上一篇: 什么是在c + +明确的关键字?

    下一篇: 什么c + +内联显式构造函数是好的?