www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Virtual member template functions (somewhat) allowed

reply Sean Kelly <sean f4.ca> writes:
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
Apr 15 2005
parent Thomas Kuehne <thomas-dloop kuehne.thisisspam.cn> writes:
-----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-----
Apr 23 2005