Tuesday, 15 May 2012

c++11 - Rercusive template for processing type of each argument -


This is a common function for creating objects of the given type. Thanks to the Variadics templates if a matching constructor is found T will be created, otherwise the compiler throws an error at compile-time.

  template & lt; Typename t, typename ... args & gt; Zero * build (RGS ... Args) {Return New T (Argus ...); }   

Now suppose that instead of passing the N parameter, I want to get them from some other place, for example, by singleton, then expanding into some kind of arguments And will have to make a method based call on the type of argument:

  class singleton {public: template & lt; Typename T & gt; Static T * Eyeses () {Return Retreat_cast < T * & gt; (ITunes_map [Typid (t). Has_code ()]); }};   

Now assume that I call

  MyClass * p = build & lt; MyClass, Foo, Bar & gt; ();   

What I want to do is that the MyClass Constructor is called, and related items of Foo and Bar are obtained from singleton and should be an example of extended template code:

  MyClass * p = new MyClass (singleton :: plus easiest & lt; foo & gt; (), singleton :: milestones & lt; bar & gt; ());   

I think that to solve that problem I need a recursive template with Tupal but it's just a feeling.

  template & lt; Typename t, typename ... args & gt; T * Build () {New T {Singleton :: Reverse & Lt; ARGS & gt; () ...}; }    

No comments:

Post a Comment