[Issue 3968] New: Some way to do certain implicit casts with operator overloading
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Mar 15 10:29:35 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3968
Summary: Some way to do certain implicit casts with operator
overloading
Product: D
Version: 2.041
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2010-03-15 10:29:34 PDT ---
Here an implicit cast to uint or int can be handy, to avoid the cast (to be
able to create a generic "fake int" struct, so for example it can be possible
to create a multiprecision integer that acts like a built-in integer), is this
possible?
struct Foo {
int x;
int opCast(T:int)() {
return this.x;
}
}
void main() {
Foo f = Foo(5);
auto a1 = new int[cast(int)f]; // OK
auto a2 = new int[f]; // ERR
}
An implicit cast can be useful in other situations (but I don't know about its
possible bad side effects):
struct Foo {
int x;
int opCast(T:int)() {
return this.x;
}
}
void bar(int i) {}
void main() {
Foo f = Foo(5);
bar(f); // Error
}
Steven Schveighoffer has suggested some possible solutions, like:
struct Foo {
int x;
uint opCast(T:uint)() { return this.x; }
int opCast(T:int)() { return this.x; }
alias opCast this;
}
void main() {
Foo f = Foo(5);
int[] a = new int[f]; // line 9
}
Or:
struct Foo {
uint x;
uint castToUint() { return x; }
alias castToUint this;
}
But they don't work. He says at least something like this should:
alias opCast!uint this;
I think that an implicit cast can also lead to bugs, so this has to be designed
with care.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list