title: C++ store template parameter as variable
body: <p>I'm writing a generic wrapper class for a bunch of classes we have defined in other code. I'm trying to figure out if there is a way in C++ to write a templated class that can store a template parameter as a variable. Here is an example of what I would like to see work:</p>
  
  <pre><code>template &lt; class C &gt; class generic {
      public:
          C obj; // used for custom type processing
  
          template &lt; class T &gt; T other; // &lt;- changeable variable (or something)
  
          template &lt; class T &gt; setOther() { // Function to change variable
              // code to change variable type, or set type
          }
  
          void doSomethingWithOther() {
              // do some processing for other, like call
              // specific member function or something
              (((other*)obj)-&gt;*SomeGenericMethod)()
          }
  }
  
  int main(int argc, char **argv) {
      Bar bObj;
      generic&lt;Foo&gt; fObj;
      fObj.setOther&lt;Bar&gt;();
      fObj.doSomethingWithOther();
      return 0;
  }
  </code></pre>
  
  <p>I know the above code won't compile for many reasons, but my question is more in regards to functionality. Is there some way that I could store a template parameter as a variable (or use a function or something) that could then be used later as a type specifier? If there is not a standard way, any ideas from the SO community that might lead to my desired results?</p>
  
  <p>Please no Boost or other 3rd party libraries, I'm trying to keep it as "STL" (C99) as possible.</p>
  
  <p>Another idea I had was to use default specializations:</p>
  
  <pre><code>template &lt; class C, class T = void* &gt; class generic {
      public:
          C obj;
          T other;
          // ... more code
  }
  </code></pre>
  
  <p>but in that instance, assuming we had the following:</p>
  
  <pre><code>class Test1 {
      public:
          generic&lt;Foo&gt; *G1;
          generic&lt;Foo, Bar&gt; *G2;
  };
  </code></pre>
  
  <p>If I wanted to change the type of G1 later to do some other processing, I could say something like the following:</p>
  
  <pre><code>G1 = new generic&lt;Foo, Bar&gt;(); // compile error, different types
  </code></pre>
  
  <p>However, this would result in a compile error since the two types pointed to are of different types (G1 is of type <code>generic&lt; C &gt;</code> the other is of type <code>generic&lt; C, T &gt;</code> even though template class <code>T</code> is defaulted to <code>void*</code>.</p>
  
  <p>Sorry for the long post, I am trying to be as clear as possible in my intentions with this code. As well, please feel free to correct me in any of my statements above (long days=less brain activity for me)</p>
  
  <p>Thanks in advance for any help in this matter.</p>
  
  <p>Edit 1:</p>
  
  <p>To clarify, the <code>other</code> data member is merely there to show that I would like that member to hold a class type. This is because our code has members of other classes calling instanced member functions. And if I were to make a call to a member function of a different class I will get a compile error.</p>
  
  <p>And unfortunately I cannot use any C++0x features as our embedded compilers are older and do not support the 0x feature set. Otherwise that would be the obvious solution.</p>
  
  <p>I am essentially having to build in a delegate type of system so that instanced classes can call members of other instanced classes from within themselves and that called member function will have the memory space and access to the <code>this</code> pointer</p>
  
  <p>Example:</p>
  
  <pre><code>class Foo {
      public:
          Delegate other;
  }
  
  class Bar {
      public:
          void SomeFunction() {
              std::cout &lt;&lt; "hello from Bar::SomeFunction, data = " &lt;&lt; data &lt;&lt; std::endl;
          }
  
          int data;
  }
  
  int main() {
       Foo F;
       Bar B;
       B.data = 10;
       F.other = Delegate::attach&lt;Bar&gt;(&amp;Bar::SomeFunction, &amp;B);
       // Delegate::attach is defined as 
       // template &lt; class T &gt; Delegate attach(void (T::*fnPtr)(void*), T *obj)
       F.other();
  }
  </code></pre>
  
  <p>In the above example <code>void operator()()</code> of the <code>Delegate</code> class then calls the other members function, via <code>(*fnPtr)()</code>. If called this way, the function has no access to it's member data (no knowledge of the <code>this</code> pointer). If called passing the referenced object (i.e. <code>(*fnPtr)(obj)</code>), the function now has access to the member data. </p>
  
  <p>I'm trying to solve this via specifying a generic <code>class type</code> object that could then be casted back and forth as the <code>Delegate other</code> may attach to any member.</p>