www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template bugged?

reply Patrick Kreft <patrick_kreft gmx.net> writes:
Is there an error?
I wrote down some ideas which i got for c++.



private import std.stdio;

class window {
     public void msgloop() {
     }
}

class application(applogic, subsystem)
{
     this() {
         m_app = new applogic();
         m_sub = new subsystem();
     }

     public int execute() {
         m_app.run();
         m_sub.msgloop();
         return 0;
     }

     private {
     applogic m_app;
     subsystem m_sub;
     }
}


class DoHello {
     public void run() {
         writefln("Hello");
     }
}


class HelloWorld : public application!(DoHello, window) {
}

class HelloWorld2 : public application!(HelloWorld2, window) {
     public void run() {
         writefln("Hello");
     }
}

int main() {
     scope auto app = new HelloWorld(); // as HelloWorld2 Error at 
Runtime: StackOverflow
     return app.execute();
}
Sep 18 2007
next sibling parent Sean Kelly <sean f4.ca> writes:
Patrick Kreft wrote:
 Is there an error?

Looks fine to me. Sean
Sep 18 2007
prev sibling parent reply BCS <ao pathlink.com> writes:
Reply to Patrick,

lets rearrange that a bit

class application(applogic)
{
  this() { new applogic(); }
}

class HelloWorld2 : public application!(HelloWorld2)
{
}

int main() { new HelloWorld2(); }

now without the template stuff

class application(applogic)
{
  this() { new HelloWorld2(); }
}

class HelloWorld2 : public application
{
  //implict this() made explict
  this(){super();}
}

int main() { new HelloWorld2(); }

new HelloWorld2 calls HelloWorld2.this 
HelloWorld2.this calls application.this
application.this calls new HelloWorld2

loop
Sep 18 2007
parent reply Patrick Kreft <patrick_kreft gmx.net> writes:
Ok i like C++ Template more :)


BCS schrieb:
 Reply to Patrick,
 
 lets rearrange that a bit
 
 class application(applogic)
 {
  this() { new applogic(); }
 }
 
 class HelloWorld2 : public application!(HelloWorld2)
 {
 }
 
 int main() { new HelloWorld2(); }
 
 now without the template stuff
 
 class application(applogic)
 {
  this() { new HelloWorld2(); }
 }
 
 class HelloWorld2 : public application
 {
  //implict this() made explict
  this(){super();}
 }
 
 int main() { new HelloWorld2(); }
 
 new HelloWorld2 calls HelloWorld2.this HelloWorld2.this calls 
 application.this
 application.this calls new HelloWorld2
 
 loop
 
 

Sep 18 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Patrick,

 Ok i like C++ Template more :)
 
 BCS schrieb:
 
 Reply to Patrick,
 
 lets rearrange that a bit
 
 class application(applogic)
 {
 this() { new applogic(); }
 }
 class HelloWorld2 : public application!(HelloWorld2)
 {
 }
 int main() { new HelloWorld2(); }
 
 now without the template stuff
 
 class application(applogic)
 {
 this() { new HelloWorld2(); }
 }
 class HelloWorld2 : public application
 {
 //implict this() made explict
 this(){super();}
 }
 int main() { new HelloWorld2(); }
 
 new HelloWorld2 calls HelloWorld2.this HelloWorld2.this calls
 application.this
 application.this calls new HelloWorld2
 loop
 


I think that would also fail in C++. If not, than the difference is not a template issue. The issue is that a class news an instance of it's self for each instance of it's self. It's just hidden by the use of template base classes
Sep 18 2007
parent reply Patrick Kreft <patrick_kreft gmx.net> writes:
BCS schrieb:
 Reply to Patrick,
 

 
 I think that would also fail in C++. If not, than the difference is not 
 a template issue. The issue is that a class news an instance of it's 
 self for each instance of it's self. It's just hidden by the use of 
 template base classes
 
 

Hmm it's work well on MinGW. #include <iostream> using namespace std; class windows { public: void msgloop() { std::cout << "MsgLoop" << std::endl; } }; template <class app, class subsystem = windows> class application { public: int execute() { _app->run(); _subsystem->msgloop(); return 0; } public: subsystem * _subsystem; app * _app; }; class HelloWorld : public application<HelloWorld> { public: void run() { std::cout << "HelloWorld" << std::endl; } }; int main() { HelloWorld app1; app1.execute(); std::cin.get(); return 0; }
Sep 18 2007
parent Patrick Kreft <patrick_kreft gmx.net> writes:
Patrick Kreft schrieb:
 BCS schrieb:
 Reply to Patrick,

 I think that would also fail in C++. If not, than the difference is 
 not a template issue. The issue is that a class news an instance of 
 it's self for each instance of it's self. It's just hidden by the use 
 of template base classes

Hmm it's work well on MinGW. #include <iostream> using namespace std; class windows { public: void msgloop() { std::cout << "MsgLoop" << std::endl; } }; template <class app, class subsystem = windows> class application { public: int execute() { _app->run(); _subsystem->msgloop(); return 0; } public: subsystem * _subsystem; app * _app; }; class HelloWorld : public application<HelloWorld> { public: void run() { std::cout << "HelloWorld" << std::endl; } }; int main() { HelloWorld app1; app1.execute(); std::cin.get(); return 0; }

ops i forget somewhat, nvm. and thx for help :)
Sep 18 2007