December 09, 2004 Another template oddity? | ||||
---|---|---|---|---|
| ||||
template TVector(T,uint SZ) { class Vector { T[SZ] values; Vector MyFunct(T f) { return new Vector(); } } } class V3f : TVector!(float,3).Vector {} void main( char[][] arg ) { V3f v = new V3f(); v = v.MyFunct(100.0f); } ... compiler gives me: > "C:\dmd\bin\dmd.exe" -debug -c E:\proj\temp\bug2.d E:\proj\temp\bug2.d(23): cannot implicitly convert expression cast(Vector )(v).MyFunct(100) of type Vector to V3f Should not this conversion be implicit? |
December 09, 2004 Re: Another template oddity? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | "David Medlock" <amedlock@nospam.org> wrote in message news:cp9ng4$c9p$1@digitaldaemon.com... > template TVector(T,uint SZ) > { > class Vector > { > T[SZ] values; > > Vector MyFunct(T f) > { > return new Vector(); > } > } > } > > class V3f : TVector!(float,3).Vector {} > > void main( char[][] arg ) > { > V3f v = new V3f(); > v = v.MyFunct(100.0f); > } > > ... > compiler gives me: > > > "C:\dmd\bin\dmd.exe" -debug -c E:\proj\temp\bug2.d > E:\proj\temp\bug2.d(23): cannot implicitly convert expression > cast(Vector )(v).MyFunct(100) of type Vector to V3f > > Should not this conversion be implicit? The return type of MyFunct is Vector but it is assigning to a variable of type V3f, which is not allowed without a cast. It is like class Foo; class Bar : Foo; ... Bar x; ... x = new Foo; ... But this is allowed: Foo y; y = new Bar; By the way, your example can be shortened to: class Vector(T,uint SZ) { T[SZ] values; Vector MyFunct(T f) { return new Vector(); } } class V3f : Vector!(float,3) {} void main( char[][] arg ) { V3f v = new V3f(); v = v.MyFunct(100.0f); } |
Copyright © 1999-2021 by the D Language Foundation