Using ternary operator in c++ returns false condition always [duplicate] The Next CEO of Stack OverflowHow to compare two BSTRs or CComBSTRs?How to compare BSTR against a string in c/c++?C++ string literal equality check?What is the “-->” operator in C++?Boolean variables aren't always false by default?Ternary conditional and assignment operator precedenceReturn type of '?:' (ternary conditional operator)Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsIs the operation “false < true” well defined?C++ comparison operator not returning true or falseWhy does the ternary operator with commas evaluate only one expression in the true case?Does the C++ standard allow for an uninitialized bool to crash a program?Why and when does the ternary operator return an lvalue?
Why is quantifier elimination desirable for a given theory?
Is it my responsibility to learn a new technology in my own time my employer wants to implement?
Reference request: Grassmannian and Plucker coordinates in type B, C, D
Does increasing your ability score affect your main stat?
Break Away Valves for Launch
Domestic-to-international connection at Orlando (MCO)
Is it possible to replace duplicates of a character with one character using tr
Why is information "lost" when it got into a black hole?
Why do airplanes bank sharply to the right after air-to-air refueling?
No sign flipping while figuring out the emf of voltaic cell?
Are police here, aren't itthey?
Would a completely good Muggle be able to use a wand?
Is micro rebar a better way to reinforce concrete than rebar?
Why doesn't UK go for the same deal Japan has with EU to resolve Brexit?
Why, when going from special to general relativity, do we just replace partial derivatives with covariant derivatives?
Flying from Cape Town to England and return to another province
Why is my new battery behaving weirdly?
What does "Its cash flow is deeply negative" mean?
Why don't programming languages automatically manage the synchronous/asynchronous problem?
I want to delete every two lines after 3rd lines in file contain very large number of lines :
How to install OpenCV on Raspbian Stretch?
Solving system of ODEs with extra parameter
Running a General Election and the European Elections together
How a 64-bit process virtual address space is divided in Linux?
Using ternary operator in c++ returns false condition always [duplicate]
The Next CEO of Stack OverflowHow to compare two BSTRs or CComBSTRs?How to compare BSTR against a string in c/c++?C++ string literal equality check?What is the “-->” operator in C++?Boolean variables aren't always false by default?Ternary conditional and assignment operator precedenceReturn type of '?:' (ternary conditional operator)Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsIs the operation “false < true” well defined?C++ comparison operator not returning true or falseWhy does the ternary operator with commas evaluate only one expression in the true case?Does the C++ standard allow for an uninitialized bool to crash a program?Why and when does the ternary operator return an lvalue?
This question already has an answer here:
How to compare two BSTRs or CComBSTRs?
3 answers
I am using a statement with ternary operator which is always returning the other value.
BSTR pVal = L"Yes";
bool val = pVal == L"Yes" ? true : false;
this statement returns
val = false;
I expect it to return true here. Am i making it wrong?
c++
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 21 at 17:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to compare two BSTRs or CComBSTRs?
3 answers
I am using a statement with ternary operator which is always returning the other value.
BSTR pVal = L"Yes";
bool val = pVal == L"Yes" ? true : false;
this statement returns
val = false;
I expect it to return true here. Am i making it wrong?
c++
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 21 at 17:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
BSTRtypes cannot be compared directly using==.
– πάντα ῥεῖ
Mar 21 at 17:57
2
Unrelated to your issue, but this is not a valid way to create aBSTR. They're supposed to be allocated withSysAllocString. In C++ it's best to use a wrapper type like_bstr_t, which will also solve your comparison issues.
– interjay
Mar 21 at 17:59
How to compare BSTR against a string
– R Sahu
Mar 21 at 18:00
Not a bug, buta == b ? true : falseis usually writtena == b.
– molbdnilo
Mar 21 at 18:08
add a comment |
This question already has an answer here:
How to compare two BSTRs or CComBSTRs?
3 answers
I am using a statement with ternary operator which is always returning the other value.
BSTR pVal = L"Yes";
bool val = pVal == L"Yes" ? true : false;
this statement returns
val = false;
I expect it to return true here. Am i making it wrong?
c++
This question already has an answer here:
How to compare two BSTRs or CComBSTRs?
3 answers
I am using a statement with ternary operator which is always returning the other value.
BSTR pVal = L"Yes";
bool val = pVal == L"Yes" ? true : false;
this statement returns
val = false;
I expect it to return true here. Am i making it wrong?
This question already has an answer here:
How to compare two BSTRs or CComBSTRs?
3 answers
c++
c++
asked Mar 21 at 17:54
S.Frank RicharrdS.Frank Richarrd
1049
1049
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 21 at 17:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 21 at 17:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
BSTRtypes cannot be compared directly using==.
– πάντα ῥεῖ
Mar 21 at 17:57
2
Unrelated to your issue, but this is not a valid way to create aBSTR. They're supposed to be allocated withSysAllocString. In C++ it's best to use a wrapper type like_bstr_t, which will also solve your comparison issues.
– interjay
Mar 21 at 17:59
How to compare BSTR against a string
– R Sahu
Mar 21 at 18:00
Not a bug, buta == b ? true : falseis usually writtena == b.
– molbdnilo
Mar 21 at 18:08
add a comment |
BSTRtypes cannot be compared directly using==.
– πάντα ῥεῖ
Mar 21 at 17:57
2
Unrelated to your issue, but this is not a valid way to create aBSTR. They're supposed to be allocated withSysAllocString. In C++ it's best to use a wrapper type like_bstr_t, which will also solve your comparison issues.
– interjay
Mar 21 at 17:59
How to compare BSTR against a string
– R Sahu
Mar 21 at 18:00
Not a bug, buta == b ? true : falseis usually writtena == b.
– molbdnilo
Mar 21 at 18:08
BSTR types cannot be compared directly using ==.– πάντα ῥεῖ
Mar 21 at 17:57
BSTR types cannot be compared directly using ==.– πάντα ῥεῖ
Mar 21 at 17:57
2
2
Unrelated to your issue, but this is not a valid way to create a
BSTR. They're supposed to be allocated with SysAllocString. In C++ it's best to use a wrapper type like _bstr_t, which will also solve your comparison issues.– interjay
Mar 21 at 17:59
Unrelated to your issue, but this is not a valid way to create a
BSTR. They're supposed to be allocated with SysAllocString. In C++ it's best to use a wrapper type like _bstr_t, which will also solve your comparison issues.– interjay
Mar 21 at 17:59
How to compare BSTR against a string
– R Sahu
Mar 21 at 18:00
How to compare BSTR against a string
– R Sahu
Mar 21 at 18:00
Not a bug, but
a == b ? true : false is usually written a == b.– molbdnilo
Mar 21 at 18:08
Not a bug, but
a == b ? true : false is usually written a == b.– molbdnilo
Mar 21 at 18:08
add a comment |
1 Answer
1
active
oldest
votes
A lot here is wrong.
== on BSTR compares pointer values. Two L"Yes" being pointer-equal is implementation defined.
bool_expr?true:false is a noop.
bool val = wcscmp(pVal, L"Yes") == 0;
is probably what you want.
BSTR pVal = L"Yes";
is bad news; that isn't how you are supposed to create a BSTR. BSTR are supposed to be pascal-esque strings allocated in a specific region of memory.
BSTR pVal = SysAllocString(L"Yes");
is usually better.
Using wcscmp is valid on a BSTR, as BSTR is guaranteed null terminated. But assigning a null terminated string to a BSTR is not a good plan; the code consuming the BSTR may expect the length field to come before the first character.
It may even be a good plan to use _bstr_t which is a C++ wrapper around BSTR.
It is also usually a good idea to compare BSTR to BSTR and not using C APIs; a BSTR can contain embedded NULs, and C APIs stop at the first NUL.
_bstr_t pVal = L"Yes";
bool val = pVal == _bstr_t(L"Yes");
_bstr_t has an operator== that does a reasonable thing.
bstr_tis a completely diferent type than wide string, one need to use the (Windows) OS conversion functions to convert one into another.
– πάντα ῥεῖ
Mar 21 at 18:00
@πάνταῥεῖ Not completely different (you can use wcscmp), but yes, I added that.
– Yakk - Adam Nevraumont
Mar 21 at 18:03
1
You cannot safely use the C string functions. BSTRs can contain embedded NUL characters. They are Pascal-style strings, with the length stored inside the string. Not C-style strings with sentinel terminator characters.
– Cody Gray♦
Mar 21 at 18:09
@CodyGray Part of that depends on what you want==to do, but yes, probably wrapping the otherL"Yes"in a BSTR and using BSTR-approved comparisons is a good plan. On the other hand, I can guarantee I can find an issue with every single==on two strings; there are languages where multiple characters become fewer in other cases, combinations of characters codes that can be treated identically or not with other characters, etc.
– Yakk - Adam Nevraumont
Mar 21 at 18:15
Yeah, just to be clear, it wasn't a criticism of your answer so much as a response to your discussion with πάντα in the comments. In this very limited case, where you're comparing to a string literal, it would obviously be fine to usewcscmp. I just wanted to explain why it's not a valid approach in general.
– Cody Gray♦
Mar 21 at 20:38
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A lot here is wrong.
== on BSTR compares pointer values. Two L"Yes" being pointer-equal is implementation defined.
bool_expr?true:false is a noop.
bool val = wcscmp(pVal, L"Yes") == 0;
is probably what you want.
BSTR pVal = L"Yes";
is bad news; that isn't how you are supposed to create a BSTR. BSTR are supposed to be pascal-esque strings allocated in a specific region of memory.
BSTR pVal = SysAllocString(L"Yes");
is usually better.
Using wcscmp is valid on a BSTR, as BSTR is guaranteed null terminated. But assigning a null terminated string to a BSTR is not a good plan; the code consuming the BSTR may expect the length field to come before the first character.
It may even be a good plan to use _bstr_t which is a C++ wrapper around BSTR.
It is also usually a good idea to compare BSTR to BSTR and not using C APIs; a BSTR can contain embedded NULs, and C APIs stop at the first NUL.
_bstr_t pVal = L"Yes";
bool val = pVal == _bstr_t(L"Yes");
_bstr_t has an operator== that does a reasonable thing.
bstr_tis a completely diferent type than wide string, one need to use the (Windows) OS conversion functions to convert one into another.
– πάντα ῥεῖ
Mar 21 at 18:00
@πάνταῥεῖ Not completely different (you can use wcscmp), but yes, I added that.
– Yakk - Adam Nevraumont
Mar 21 at 18:03
1
You cannot safely use the C string functions. BSTRs can contain embedded NUL characters. They are Pascal-style strings, with the length stored inside the string. Not C-style strings with sentinel terminator characters.
– Cody Gray♦
Mar 21 at 18:09
@CodyGray Part of that depends on what you want==to do, but yes, probably wrapping the otherL"Yes"in a BSTR and using BSTR-approved comparisons is a good plan. On the other hand, I can guarantee I can find an issue with every single==on two strings; there are languages where multiple characters become fewer in other cases, combinations of characters codes that can be treated identically or not with other characters, etc.
– Yakk - Adam Nevraumont
Mar 21 at 18:15
Yeah, just to be clear, it wasn't a criticism of your answer so much as a response to your discussion with πάντα in the comments. In this very limited case, where you're comparing to a string literal, it would obviously be fine to usewcscmp. I just wanted to explain why it's not a valid approach in general.
– Cody Gray♦
Mar 21 at 20:38
|
show 1 more comment
A lot here is wrong.
== on BSTR compares pointer values. Two L"Yes" being pointer-equal is implementation defined.
bool_expr?true:false is a noop.
bool val = wcscmp(pVal, L"Yes") == 0;
is probably what you want.
BSTR pVal = L"Yes";
is bad news; that isn't how you are supposed to create a BSTR. BSTR are supposed to be pascal-esque strings allocated in a specific region of memory.
BSTR pVal = SysAllocString(L"Yes");
is usually better.
Using wcscmp is valid on a BSTR, as BSTR is guaranteed null terminated. But assigning a null terminated string to a BSTR is not a good plan; the code consuming the BSTR may expect the length field to come before the first character.
It may even be a good plan to use _bstr_t which is a C++ wrapper around BSTR.
It is also usually a good idea to compare BSTR to BSTR and not using C APIs; a BSTR can contain embedded NULs, and C APIs stop at the first NUL.
_bstr_t pVal = L"Yes";
bool val = pVal == _bstr_t(L"Yes");
_bstr_t has an operator== that does a reasonable thing.
bstr_tis a completely diferent type than wide string, one need to use the (Windows) OS conversion functions to convert one into another.
– πάντα ῥεῖ
Mar 21 at 18:00
@πάνταῥεῖ Not completely different (you can use wcscmp), but yes, I added that.
– Yakk - Adam Nevraumont
Mar 21 at 18:03
1
You cannot safely use the C string functions. BSTRs can contain embedded NUL characters. They are Pascal-style strings, with the length stored inside the string. Not C-style strings with sentinel terminator characters.
– Cody Gray♦
Mar 21 at 18:09
@CodyGray Part of that depends on what you want==to do, but yes, probably wrapping the otherL"Yes"in a BSTR and using BSTR-approved comparisons is a good plan. On the other hand, I can guarantee I can find an issue with every single==on two strings; there are languages where multiple characters become fewer in other cases, combinations of characters codes that can be treated identically or not with other characters, etc.
– Yakk - Adam Nevraumont
Mar 21 at 18:15
Yeah, just to be clear, it wasn't a criticism of your answer so much as a response to your discussion with πάντα in the comments. In this very limited case, where you're comparing to a string literal, it would obviously be fine to usewcscmp. I just wanted to explain why it's not a valid approach in general.
– Cody Gray♦
Mar 21 at 20:38
|
show 1 more comment
A lot here is wrong.
== on BSTR compares pointer values. Two L"Yes" being pointer-equal is implementation defined.
bool_expr?true:false is a noop.
bool val = wcscmp(pVal, L"Yes") == 0;
is probably what you want.
BSTR pVal = L"Yes";
is bad news; that isn't how you are supposed to create a BSTR. BSTR are supposed to be pascal-esque strings allocated in a specific region of memory.
BSTR pVal = SysAllocString(L"Yes");
is usually better.
Using wcscmp is valid on a BSTR, as BSTR is guaranteed null terminated. But assigning a null terminated string to a BSTR is not a good plan; the code consuming the BSTR may expect the length field to come before the first character.
It may even be a good plan to use _bstr_t which is a C++ wrapper around BSTR.
It is also usually a good idea to compare BSTR to BSTR and not using C APIs; a BSTR can contain embedded NULs, and C APIs stop at the first NUL.
_bstr_t pVal = L"Yes";
bool val = pVal == _bstr_t(L"Yes");
_bstr_t has an operator== that does a reasonable thing.
A lot here is wrong.
== on BSTR compares pointer values. Two L"Yes" being pointer-equal is implementation defined.
bool_expr?true:false is a noop.
bool val = wcscmp(pVal, L"Yes") == 0;
is probably what you want.
BSTR pVal = L"Yes";
is bad news; that isn't how you are supposed to create a BSTR. BSTR are supposed to be pascal-esque strings allocated in a specific region of memory.
BSTR pVal = SysAllocString(L"Yes");
is usually better.
Using wcscmp is valid on a BSTR, as BSTR is guaranteed null terminated. But assigning a null terminated string to a BSTR is not a good plan; the code consuming the BSTR may expect the length field to come before the first character.
It may even be a good plan to use _bstr_t which is a C++ wrapper around BSTR.
It is also usually a good idea to compare BSTR to BSTR and not using C APIs; a BSTR can contain embedded NULs, and C APIs stop at the first NUL.
_bstr_t pVal = L"Yes";
bool val = pVal == _bstr_t(L"Yes");
_bstr_t has an operator== that does a reasonable thing.
edited Mar 21 at 18:17
answered Mar 21 at 17:58
Yakk - Adam NevraumontYakk - Adam Nevraumont
188k21199384
188k21199384
bstr_tis a completely diferent type than wide string, one need to use the (Windows) OS conversion functions to convert one into another.
– πάντα ῥεῖ
Mar 21 at 18:00
@πάνταῥεῖ Not completely different (you can use wcscmp), but yes, I added that.
– Yakk - Adam Nevraumont
Mar 21 at 18:03
1
You cannot safely use the C string functions. BSTRs can contain embedded NUL characters. They are Pascal-style strings, with the length stored inside the string. Not C-style strings with sentinel terminator characters.
– Cody Gray♦
Mar 21 at 18:09
@CodyGray Part of that depends on what you want==to do, but yes, probably wrapping the otherL"Yes"in a BSTR and using BSTR-approved comparisons is a good plan. On the other hand, I can guarantee I can find an issue with every single==on two strings; there are languages where multiple characters become fewer in other cases, combinations of characters codes that can be treated identically or not with other characters, etc.
– Yakk - Adam Nevraumont
Mar 21 at 18:15
Yeah, just to be clear, it wasn't a criticism of your answer so much as a response to your discussion with πάντα in the comments. In this very limited case, where you're comparing to a string literal, it would obviously be fine to usewcscmp. I just wanted to explain why it's not a valid approach in general.
– Cody Gray♦
Mar 21 at 20:38
|
show 1 more comment
bstr_tis a completely diferent type than wide string, one need to use the (Windows) OS conversion functions to convert one into another.
– πάντα ῥεῖ
Mar 21 at 18:00
@πάνταῥεῖ Not completely different (you can use wcscmp), but yes, I added that.
– Yakk - Adam Nevraumont
Mar 21 at 18:03
1
You cannot safely use the C string functions. BSTRs can contain embedded NUL characters. They are Pascal-style strings, with the length stored inside the string. Not C-style strings with sentinel terminator characters.
– Cody Gray♦
Mar 21 at 18:09
@CodyGray Part of that depends on what you want==to do, but yes, probably wrapping the otherL"Yes"in a BSTR and using BSTR-approved comparisons is a good plan. On the other hand, I can guarantee I can find an issue with every single==on two strings; there are languages where multiple characters become fewer in other cases, combinations of characters codes that can be treated identically or not with other characters, etc.
– Yakk - Adam Nevraumont
Mar 21 at 18:15
Yeah, just to be clear, it wasn't a criticism of your answer so much as a response to your discussion with πάντα in the comments. In this very limited case, where you're comparing to a string literal, it would obviously be fine to usewcscmp. I just wanted to explain why it's not a valid approach in general.
– Cody Gray♦
Mar 21 at 20:38
bstr_t is a completely diferent type than wide string, one need to use the (Windows) OS conversion functions to convert one into another.– πάντα ῥεῖ
Mar 21 at 18:00
bstr_t is a completely diferent type than wide string, one need to use the (Windows) OS conversion functions to convert one into another.– πάντα ῥεῖ
Mar 21 at 18:00
@πάνταῥεῖ Not completely different (you can use wcscmp), but yes, I added that.
– Yakk - Adam Nevraumont
Mar 21 at 18:03
@πάνταῥεῖ Not completely different (you can use wcscmp), but yes, I added that.
– Yakk - Adam Nevraumont
Mar 21 at 18:03
1
1
You cannot safely use the C string functions. BSTRs can contain embedded NUL characters. They are Pascal-style strings, with the length stored inside the string. Not C-style strings with sentinel terminator characters.
– Cody Gray♦
Mar 21 at 18:09
You cannot safely use the C string functions. BSTRs can contain embedded NUL characters. They are Pascal-style strings, with the length stored inside the string. Not C-style strings with sentinel terminator characters.
– Cody Gray♦
Mar 21 at 18:09
@CodyGray Part of that depends on what you want
== to do, but yes, probably wrapping the other L"Yes" in a BSTR and using BSTR-approved comparisons is a good plan. On the other hand, I can guarantee I can find an issue with every single == on two strings; there are languages where multiple characters become fewer in other cases, combinations of characters codes that can be treated identically or not with other characters, etc.– Yakk - Adam Nevraumont
Mar 21 at 18:15
@CodyGray Part of that depends on what you want
== to do, but yes, probably wrapping the other L"Yes" in a BSTR and using BSTR-approved comparisons is a good plan. On the other hand, I can guarantee I can find an issue with every single == on two strings; there are languages where multiple characters become fewer in other cases, combinations of characters codes that can be treated identically or not with other characters, etc.– Yakk - Adam Nevraumont
Mar 21 at 18:15
Yeah, just to be clear, it wasn't a criticism of your answer so much as a response to your discussion with πάντα in the comments. In this very limited case, where you're comparing to a string literal, it would obviously be fine to use
wcscmp. I just wanted to explain why it's not a valid approach in general.– Cody Gray♦
Mar 21 at 20:38
Yeah, just to be clear, it wasn't a criticism of your answer so much as a response to your discussion with πάντα in the comments. In this very limited case, where you're comparing to a string literal, it would obviously be fine to use
wcscmp. I just wanted to explain why it's not a valid approach in general.– Cody Gray♦
Mar 21 at 20:38
|
show 1 more comment
BSTRtypes cannot be compared directly using==.– πάντα ῥεῖ
Mar 21 at 17:57
2
Unrelated to your issue, but this is not a valid way to create a
BSTR. They're supposed to be allocated withSysAllocString. In C++ it's best to use a wrapper type like_bstr_t, which will also solve your comparison issues.– interjay
Mar 21 at 17:59
How to compare BSTR against a string
– R Sahu
Mar 21 at 18:00
Not a bug, but
a == b ? true : falseis usually writtena == b.– molbdnilo
Mar 21 at 18:08