473,796 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

implicit operator bool - question 2

Why would you ever want to define both "public static bool operator
true" and "public static bool operator false" when you can just define
"public static implicit operator bool"?

Is there syntax where the two are not equivalent? Or are there cases
where you might legitimately want both false and true to return the
same value?

Fwiw, I'm having a hard time coming up with any C# 1.1 code that calls
true or false operators. I'm inclined to fault my understanding
because I'm generally very impressed with the quality of the C# design
and implementation - but it sure LOOKS like "operator true" and
"operator" false are just some sort of design archaeology that never
got thought through ....

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 15 '05 #1
3 1988

"Jon Shemitz" <jo*@midnightbe ach.com> wrote in message
news:40******** *******@midnigh tbeach.com...
Why would you ever want to define both "public static bool operator
true" and "public static bool operator false" when you can just define
"public static implicit operator bool"?

Is there syntax where the two are not equivalent? Or are there cases
where you might legitimately want both false and true to return the
same value?

Fwiw, I'm having a hard time coming up with any C# 1.1 code that calls
true or false operators. I'm inclined to fault my understanding
because I'm generally very impressed with the quality of the C# design
and implementation - but it sure LOOKS like "operator true" and
"operator" false are just some sort of design archaeology that never
got thought through ....

I can't come up with areason for both, I'll take a closer look at the specs
for justification. However, asto code that will use the operators, you
basically need to use the ? operator:

object ? trueStatement : falseStatement; calls object.op_True if its
present. --

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 15 '05 #2

"Jon Shemitz" <jo*@midnightbe ach.com> wrote in message
news:40******** *******@midnigh tbeach.com...
Why would you ever want to define both "public static bool operator
true" and "public static bool operator false" when you can just define
"public static implicit operator bool"?

Is there syntax where the two are not equivalent? Or are there cases
where you might legitimately want both false and true to return the
same value?

Ok, I read up on the operators and there is a valid reason for there being 2
operators. Basically, it allows you to have values that are true, false,
both true and false, or niether true and false(IE null, I imagine this is
how the DB types work). There is an example using true and false operators
in the c# spec that implements a DBBool class, supporting null.

According to the spec, the && and || operators, when in the prescense of
overloaded & and |, use true and false. A call to && uses the false
operator, || the true. From the ecma C# spec(page 171, pdf page 187, section
14.11.2):

? The operation x && y is evaluated as T.false(x) ? x : T.&(x, y), where
T.false(x) is an
invocation of the operator false declared in T, and T.&(x, y) is an
invocation of the selected
operator &. In other words, x is first evaluated and operator false is
invoked on the result to
determine if x is definitely false. Then, if x is definitely false, the
result of the operation is the value
previously computed for x. Otherwise, y is evaluated, and the selected
operator & is invoked on the
value previously computed for x and the value computed for y to produce the
result of the operation.
? The operation x || y is evaluated as T.true(x) ? x : T.|(x, y), where
T.true(x) is an
invocation of the operator true declared in T, and T.|(x, y) is an
invocation of the selected
operator |. In other words, x is first evaluated and operator true is
invoked on the result to
determine if x is definitely true. Then, if x is definitely true, the result
of the operation is the value
previously computed for x. Otherwise, y is evaluated, and the selected
operator | is invoked on the
value previously computed for x and the value computed for y to produce the
result of the operation.

Situations where you'd want both true and false are pretty rare, I suspect,
however I guess it may have its value for 0(> 0 is true, < 0 false, 0 is
both, null niether) in some situations.

Fwiw, I'm having a hard time coming up with any C# 1.1 code that calls
true or false operators. I'm inclined to fault my understanding
because I'm generally very impressed with the quality of the C# design
and implementation - but it sure LOOKS like "operator true" and
"operator" false are just some sort of design archaeology that never
got thought through ....

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 15 '05 #3
"Daniel O'Connell [C# MVP]" wrote:
Ok, I read up on the operators and there is a valid reason for there being 2
operators. Basically, it allows you to have values that are true, false,
both true and false, or niether true and false(IE null, I imagine this is
how the DB types work). There is an example using true and false operators
in the c# spec that implements a DBBool class, supporting null.

According to the spec, the && and || operators, when in the prescense of
overloaded & and |, use true and false. A call to && uses the false
operator, || the true. From the ecma C# spec(page 171, pdf page 187, section
14.11.2):


Thanks - I'll experiment with this.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4334
by: Simon Ford | last post by:
Hi All, I'm having trouble understanding exactly how I can do some specific implicit casting. There are two problems here; does anyone know what I should be doing? //---------- // (1) Resolving as bool for comparison, but not as int. // // we have a class
3
1659
by: Boris | last post by:
Hello, I have written a program that compiles just fine with GCC 3.3. With GCC 3.4.1 it results in this error: g++ -O0 -g3 -Wall -c -oTestp.o ../Testp.c .../Testp.c: In function `int main()': .../Testp.c:26: error: no match for 'operator<=' in 't <= 2.0e+0' Please help me, I have no idea where the problem is.
3
2961
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the following error messages: warning: implicit typename is deprecated, please see the documentation for details Can someone kindly suggest how to get rid of these warnings? The source code follows.
2
2284
by: Jon Shemitz | last post by:
How come I can write code like "if (L)" but NOT code like "if (L == true)" when L is a class with an implicit operator bool? /////////// List L = new List(); public class List { private long count = 0;
9
2088
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and emulate the pattern im trying to follow in an existing project. These are my steps: 1) I have a method "printMe" existing in the application which originally used to take in a string. This method is static and sits in the Driver
2
1916
by: Calum Grant | last post by:
Here's a little utility I thought I'd share. (For those familiar with ATL::CComQIPtr, the concept is similar). ==== #ifndef DYNPTR_H_INCLUDED #define DYNPTR_H_INCLUDED
3
4624
by: utab | last post by:
Dear all, I was trying to write a more complex program, and while searching for sth in my reference C++ primer, by Lippman. I had a question in the mind, see the code below #include <string> #include <iostream> #include <cstring> int main(){
19
3903
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
0
1273
by: tonvandenheuvel | last post by:
Hi all, please consider the following piece of code: #include <iostream> template<typename T> class A { public:
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.