generalising template specialisation

Thomas Kuehne thomas-dloop at kuehne.cn
Sat Nov 11 08:31:02 PST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I am going to use TemplateTypeParameterSpecialization in the samples
below, all the ideas apply to TemplateValueParameterSpecialization too.


Current specialisation:
#
# template A (T : SomeType) { /* ... */ }
#

Template A matches if T is implicitly convertible to SomeType.


How do you define a template that matches if T is a struct?
#
# template B (T) {
#    static if(is(T == struct)) {
#	/* ... */
#    } else {
#       static assert(0, "use a struct");
#    }
# }
# 

OK that works, though the error will be reported on the assert line
instead of the instantiation line. Now suppose someoneelse writes a
template with the same functionality that supports classes:

#
# template B (T) {
#    static if(is(T == class)) {
#	/* ... */
#    } else {
#       static assert(0, "use a class");
#    }
# }
# 

Err, won't work:
b2.d(1): template b2.B(T) conflicts with b1.B(T) at b1.d(1)

Solution 1: merge those templates
#
# template B (T) {
#    static if(is(T == class)) {
#	/* ... */
#    } else if(is(T == struct)) {
#	/* ... */
#    } else {
#       static assert(0, "use a class or struct");
#    }
# }
# 

Problem: Merging 2 templates having different licenses and maintainers
can become a nightmare.

Solution 2: use a meta template
#
# private import b1, b2;
#
# template B (T) {
#    static if(is(T == class)) {
#	alias b1.B!(T) B;
#    } else if(is(T == struct)) {
#	alias b2.B!(T) B;
#    } else {
#       static assert(0, "use a class or struct");
#    }
# }
#

Problem: This is a very brittle approach introducing an extra level
of indirection and will fail if someone writes a third template
supporting e.g. delegates.

Please lift the current limitation and support all constructs allowed by
StaticIfCondition in TemplateTypeParameterSpecialization too.

Rewriting the B templates:
#
# template B( T : is(T == class)) {
#    /* ... */
# }
#
# template B( T : is(T == struct)) {
#    /* ... */
# }
#

I'm not aware of any lexicographic issues or problems with existing
code.

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFFVgePLK5blCcjpWoRAozeAJ0XhdYhQpU5h+ShfE20pRDZlvaCgQCfdtw4
z+eb88EAvNdxM3UDyq4dwH0=
=7J4/
-----END PGP SIGNATURE-----



More information about the Digitalmars-d mailing list