explicit specifier doesn't seem to work when converting an object to bool
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
1
Maybe reated: stackoverflow.com/questions/39995573/…
– Sergey
Jan 29 at 15:35
add a comment |
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
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
c++ type-conversion explicit
edited Jan 29 at 22:11
Tas
5,36832542
5,36832542
asked Jan 29 at 15:31
Jinlong ChenJinlong Chen
664
664
1
Maybe reated: stackoverflow.com/questions/39995573/…
– Sergey
Jan 29 at 15:35
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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 declarationbool 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
.
add a comment |
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 declarationbool t(e)
; is well-formed (that is, an explicit conversion function such asexplicit T::operator bool() const;
is considered). Such expressione
is said to be contextually converted tobool
.
- 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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 declarationbool 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
.
add a comment |
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 declarationbool 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
.
add a comment |
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 declarationbool 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
.
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 declarationbool 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
.
edited Jan 29 at 16:00
answered Jan 29 at 15:35
songyuanyaosongyuanyao
91.6k11175238
91.6k11175238
add a comment |
add a comment |
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 declarationbool t(e)
; is well-formed (that is, an explicit conversion function such asexplicit T::operator bool() const;
is considered). Such expressione
is said to be contextually converted tobool
.
- 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.
add a comment |
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 declarationbool t(e)
; is well-formed (that is, an explicit conversion function such asexplicit T::operator bool() const;
is considered). Such expressione
is said to be contextually converted tobool
.
- 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.
add a comment |
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 declarationbool t(e)
; is well-formed (that is, an explicit conversion function such asexplicit T::operator bool() const;
is considered). Such expressione
is said to be contextually converted tobool
.
- 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.
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 declarationbool t(e)
; is well-formed (that is, an explicit conversion function such asexplicit T::operator bool() const;
is considered). Such expressione
is said to be contextually converted tobool
.
- 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.
answered Jan 29 at 15:35
ShadowRangerShadowRanger
60.7k55796
60.7k55796
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Maybe reated: stackoverflow.com/questions/39995573/…
– Sergey
Jan 29 at 15:35