Using templates to switch interface implementation class

Marcin Kuszczak aarti at interia.pl
Mon May 22 11:39:06 PDT 2006


Tom S wrote:

> I'm attaching the best solution I could come up with...
> The binding is done in a static module ctor. Creating an 'implementation
> instance' for a given interface incurs a cost of a virtual function call
> and there's a minimal storage penalty, but at least it's not very
> hackish and its usage is very simple.
> 
> 

Thanks! It looks like this solution is working pretty good.


But if anyone have idea how to make it completely using static templates to
achieve same effect it would be interesting for me. Especially I wonder why
my code doesn't work as expected (It works for one binging, but not for
more):

1.      template Bind(I, T) {
2.              alias T PT;
3.              template getInstance(MI:I) {
4.                      MI getInstance() {
5.                              return new PT;
6.                      }
7.              }
8.      }

1) Why there must be alias in line 2? without this "alias" in line 5
compiler can not see T from line 1.
2) Do I understand it correctly that above code should instantiate to e.g.:
        template getInstance(MI:IMainView) {
                MI getInstance() {
                return new MainViewTest;
                }
        }
as it is not possible to put anything instead of 'MI' when instantiating?

It would  be great if someone could explain me how does it work... :-)

PS. It's good to have helpful hand in D here in Poland. Kraków sends
greetings to Toruń :-)

Regards
Marcin Kuszczak
(Aarti_pl)




More information about the Digitalmars-d-learn mailing list