explicit specifier doesn't seem to work when converting an object to bool












12















I am learning C++ recently and I noticed an example on cppreference, part of which goes like this:



struct B
{
explicit B(int) { }
explicit operator bool() const { return true; }
};

int main()
{
B b2(2); // OK: direct-initialization selects B::B(int)
if (b2) ; // OK: B::operator bool()
}


The introduction to implicit conversions tells me "when the expression is used in an if statement or a loop" the result of this expression( b2 ) will be converted into bool type implicitly.



Also, the introduction to explicit specifier tells me if "a conversion function is explicit, it cannot be used for implicit conversions".



Since b2 will be converted implicitly in if(b2), and the conversion function is explicit, how comes if(b2) is ok?










share|improve this question




















  • 1





    Maybe reated: stackoverflow.com/questions/39995573/…

    – Sergey
    Jan 29 at 15:35
















12















I am learning C++ recently and I noticed an example on cppreference, part of which goes like this:



struct B
{
explicit B(int) { }
explicit operator bool() const { return true; }
};

int main()
{
B b2(2); // OK: direct-initialization selects B::B(int)
if (b2) ; // OK: B::operator bool()
}


The introduction to implicit conversions tells me "when the expression is used in an if statement or a loop" the result of this expression( b2 ) will be converted into bool type implicitly.



Also, the introduction to explicit specifier tells me if "a conversion function is explicit, it cannot be used for implicit conversions".



Since b2 will be converted implicitly in if(b2), and the conversion function is explicit, how comes if(b2) is ok?










share|improve this question




















  • 1





    Maybe reated: stackoverflow.com/questions/39995573/…

    – Sergey
    Jan 29 at 15:35














12












12








12








I am learning C++ recently and I noticed an example on cppreference, part of which goes like this:



struct B
{
explicit B(int) { }
explicit operator bool() const { return true; }
};

int main()
{
B b2(2); // OK: direct-initialization selects B::B(int)
if (b2) ; // OK: B::operator bool()
}


The introduction to implicit conversions tells me "when the expression is used in an if statement or a loop" the result of this expression( b2 ) will be converted into bool type implicitly.



Also, the introduction to explicit specifier tells me if "a conversion function is explicit, it cannot be used for implicit conversions".



Since b2 will be converted implicitly in if(b2), and the conversion function is explicit, how comes if(b2) is ok?










share|improve this question
















I am learning C++ recently and I noticed an example on cppreference, part of which goes like this:



struct B
{
explicit B(int) { }
explicit operator bool() const { return true; }
};

int main()
{
B b2(2); // OK: direct-initialization selects B::B(int)
if (b2) ; // OK: B::operator bool()
}


The introduction to implicit conversions tells me "when the expression is used in an if statement or a loop" the result of this expression( b2 ) will be converted into bool type implicitly.



Also, the introduction to explicit specifier tells me if "a conversion function is explicit, it cannot be used for implicit conversions".



Since b2 will be converted implicitly in if(b2), and the conversion function is explicit, how comes if(b2) is ok?







c++ type-conversion explicit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 29 at 22:11









Tas

5,35832542




5,35832542










asked Jan 29 at 15:31









Jinlong ChenJinlong Chen

664




664








  • 1





    Maybe reated: stackoverflow.com/questions/39995573/…

    – Sergey
    Jan 29 at 15:35














  • 1





    Maybe reated: stackoverflow.com/questions/39995573/…

    – Sergey
    Jan 29 at 15:35








1




1





Maybe reated: stackoverflow.com/questions/39995573/…

– Sergey
Jan 29 at 15:35





Maybe reated: stackoverflow.com/questions/39995573/…

– Sergey
Jan 29 at 15:35












2 Answers
2






active

oldest

votes


















12














Contextual conversion is special; since C++11, explicit conversion functions will be considered in contextual conversions.



(emphasis mine)




(since C++11)



In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.




  • the controlling expression of if, while, for;

  • the operands of the built-in logical operators !, && and ||;

  • the first operand of the conditional operator ?:;

  • the predicate in a static_assert declaration;

  • the expression in a noexcept specifier;

  • the expression in an explicit specifier; (since C++20)

  • the predicate of a contract attribute. (since C++20)




That means for if (b2), b2 will be converted to bool implicitly by B::operator bool() even it's declared as explicit.






share|improve this answer

































    7














    Read further in your own link. Contextual conversions occur implicitly even for explicit conversions:




    Contextual conversions



    In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.




    • the controlling expression of if, while, for;

    • the operands of the built-in logical operators !, && and ||;

    • the first operand of the conditional operator ?:;

    • the predicate in a static_assert declaration;

    • the expression in a noexcept specifier;

    • the expression in an explicit specifier;

    • the predicate of a contract attribute.







    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54424445%2fexplicit-specifier-doesnt-seem-to-work-when-converting-an-object-to-bool%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      12














      Contextual conversion is special; since C++11, explicit conversion functions will be considered in contextual conversions.



      (emphasis mine)




      (since C++11)



      In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.




      • the controlling expression of if, while, for;

      • the operands of the built-in logical operators !, && and ||;

      • the first operand of the conditional operator ?:;

      • the predicate in a static_assert declaration;

      • the expression in a noexcept specifier;

      • the expression in an explicit specifier; (since C++20)

      • the predicate of a contract attribute. (since C++20)




      That means for if (b2), b2 will be converted to bool implicitly by B::operator bool() even it's declared as explicit.






      share|improve this answer






























        12














        Contextual conversion is special; since C++11, explicit conversion functions will be considered in contextual conversions.



        (emphasis mine)




        (since C++11)



        In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.




        • the controlling expression of if, while, for;

        • the operands of the built-in logical operators !, && and ||;

        • the first operand of the conditional operator ?:;

        • the predicate in a static_assert declaration;

        • the expression in a noexcept specifier;

        • the expression in an explicit specifier; (since C++20)

        • the predicate of a contract attribute. (since C++20)




        That means for if (b2), b2 will be converted to bool implicitly by B::operator bool() even it's declared as explicit.






        share|improve this answer




























          12












          12








          12







          Contextual conversion is special; since C++11, explicit conversion functions will be considered in contextual conversions.



          (emphasis mine)




          (since C++11)



          In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.




          • the controlling expression of if, while, for;

          • the operands of the built-in logical operators !, && and ||;

          • the first operand of the conditional operator ?:;

          • the predicate in a static_assert declaration;

          • the expression in a noexcept specifier;

          • the expression in an explicit specifier; (since C++20)

          • the predicate of a contract attribute. (since C++20)




          That means for if (b2), b2 will be converted to bool implicitly by B::operator bool() even it's declared as explicit.






          share|improve this answer















          Contextual conversion is special; since C++11, explicit conversion functions will be considered in contextual conversions.



          (emphasis mine)




          (since C++11)



          In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.




          • the controlling expression of if, while, for;

          • the operands of the built-in logical operators !, && and ||;

          • the first operand of the conditional operator ?:;

          • the predicate in a static_assert declaration;

          • the expression in a noexcept specifier;

          • the expression in an explicit specifier; (since C++20)

          • the predicate of a contract attribute. (since C++20)




          That means for if (b2), b2 will be converted to bool implicitly by B::operator bool() even it's declared as explicit.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 29 at 16:00

























          answered Jan 29 at 15:35









          songyuanyaosongyuanyao

          91k11173235




          91k11173235

























              7














              Read further in your own link. Contextual conversions occur implicitly even for explicit conversions:




              Contextual conversions



              In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.




              • the controlling expression of if, while, for;

              • the operands of the built-in logical operators !, && and ||;

              • the first operand of the conditional operator ?:;

              • the predicate in a static_assert declaration;

              • the expression in a noexcept specifier;

              • the expression in an explicit specifier;

              • the predicate of a contract attribute.







              share|improve this answer




























                7














                Read further in your own link. Contextual conversions occur implicitly even for explicit conversions:




                Contextual conversions



                In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.




                • the controlling expression of if, while, for;

                • the operands of the built-in logical operators !, && and ||;

                • the first operand of the conditional operator ?:;

                • the predicate in a static_assert declaration;

                • the expression in a noexcept specifier;

                • the expression in an explicit specifier;

                • the predicate of a contract attribute.







                share|improve this answer


























                  7












                  7








                  7







                  Read further in your own link. Contextual conversions occur implicitly even for explicit conversions:




                  Contextual conversions



                  In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.




                  • the controlling expression of if, while, for;

                  • the operands of the built-in logical operators !, && and ||;

                  • the first operand of the conditional operator ?:;

                  • the predicate in a static_assert declaration;

                  • the expression in a noexcept specifier;

                  • the expression in an explicit specifier;

                  • the predicate of a contract attribute.







                  share|improve this answer













                  Read further in your own link. Contextual conversions occur implicitly even for explicit conversions:




                  Contextual conversions



                  In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.




                  • the controlling expression of if, while, for;

                  • the operands of the built-in logical operators !, && and ||;

                  • the first operand of the conditional operator ?:;

                  • the predicate in a static_assert declaration;

                  • the expression in a noexcept specifier;

                  • the expression in an explicit specifier;

                  • the predicate of a contract attribute.








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 29 at 15:35









                  ShadowRangerShadowRanger

                  60.1k55796




                  60.1k55796






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54424445%2fexplicit-specifier-doesnt-seem-to-work-when-converting-an-object-to-bool%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents