April 15, 2005 Virtual member template functions (somewhat) allowed | ||||
---|---|---|---|---|
| ||||
C++ disallows virtual member templates and attempting to declare one causes a compile-time error. D doesn't support them, but it does allow same-named template functions that would normally be considered virtual. Worse yet, the "override" attribute on such functions is ignored. # import std.c.stdio; # # class C # { # template fn(T) { void fn() { printf( "base\n" ); } } # } # # class D : C # { # template fn(T) { override void fn() { printf( "derived\n" ); } } # } # # class E # { # void fn() { printf( "base\n" ); } # } # # class F : E # { # override void fn() { printf( "derived\n" ); } # } # # void main() # { # C c = new D(); # c.fn!(int)(); # # E e = new F(); # e.fn(); # } Prints: base derived Removing the definitions for fn() in C & E causes a compile-time error for F but not for D. At the very least, it would be nice if "override" were not allowed as an attribute of member template functions and the documentation indicated that such functions are implicitly final. Sean |
April 23, 2005 Re: Virtual member template functions (somewhat) allowed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Sean Kelly schrieb am Fri, 15 Apr 2005 21:02:21 +0000 (UTC): > C++ disallows virtual member templates and attempting to declare one causes a compile-time error. D doesn't support them, but it does allow same-named template functions that would normally be considered virtual. Worse yet, the "override" attribute on such functions is ignored. > > # import std.c.stdio; > # > # class C > # { > # template fn(T) { void fn() { printf( "base\n" ); } } > # } > # > # class D : C > # { > # template fn(T) { override void fn() { printf( "derived\n" ); } } > # } > # > # class E > # { > # void fn() { printf( "base\n" ); } > # } > # > # class F : E > # { > # override void fn() { printf( "derived\n" ); } > # } > # > # void main() > # { > # C c = new D(); > # c.fn!(int)(); > # > # E e = new F(); > # e.fn(); > # } > > Prints: > > base > derived > > Removing the definitions for fn() in C & E causes a compile-time error for F but > not for D. > > At the very least, it would be nice if "override" were not allowed as an attribute of member template functions and the documentation indicated that such functions are implicitly final. Added to DStress as http://dstress.kuehne.cn/override_10.d http://dstress.kuehne.cn/template_15.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCafV/3w+/yD4P9tIRAvCJAJ4hp9/raG5ws0TOEVCXlFNHDIqMNACcC6tr c65o+UwZ8zKu6MdYuB6z21c= =TeX2 -----END PGP SIGNATURE----- |
Copyright © 1999-2021 by the D Language Foundation