Is this meant to be possible?
ted
foo at bar.com
Mon Feb 17 16:59:09 PST 2014
Fantastic !! (thanks !)
so is:
createDynamicInstance(ClassInfo type) { return type.create(); }
the correct implementation ?? - it certainly seems to work...
Adam D. Ruppe wrote:
> On Tuesday, 18 February 2014 at 00:31:22 UTC, ted wrote:
>> public interface IProvider
>> {
>> string providedType();
>> T createInstance(T)();
>
> This will cause the linker problem because templates cannot be
> virtual. This is declaring a final method in the interface that
> is never implemented.
>
> The reason they can't be virtual is that an interface consists of
> an array of function pointers. Since templates might form
> multiple functions based on their compile-time arguments, the
> compiler can't know how many slots to reserve in that array for
> it.
>
>
> What you can do is something like this:
>
> interface IProvider {
> // this is a final method with an implementation right here
> T createInstance(T)() {
> auto i = cast(T) createDynamicInstance(typeid(T));
> if(i is null) throw new Exception("Couldn't create " ~
> T.stringof);
> return i;
> }
>
> string providedType(); // virtual function
> Object createDynamicInstance(ClassInfo type); // virtual
> }
>
>
> Then in the class, implement createDynamicInstance based on the
> classinfo instead of the template. Your other code for add and
> get should continue to work.
More information about the Digitalmars-d-learn
mailing list