title: dynamic_cast<> ing variable arguments to templates
body: <p>I have a C++ application that executes test cases. It is possible that some test cases will depend on output from other test cases. </p>
  
  <p>All test cases implement a basic interface:</p>
  
  <pre><code>/// base class for all test cases
  class ITest
  {
  public:
      virtual void Execute() = 0;
  };
  </code></pre>
  
  <p>Test cases that produce some object that may be useful to other test cases implement this interface: </p>
  
  <pre><code>/// implemented by test cases that provide data to other test cases
  template&lt; class Obj &gt;
  class IDependency
  {
  public:
      virtual Obj Get() = 0;
  };
  </code></pre>
  
  <p>Test cases that require data from other test cases implement this interface:</p>
  
  <pre><code>/// implemented by test cases that require data from other test cases
  template&lt; class Obj &gt;
  class IDependent
  {
  public:
  
      void SetDependency( IDependency&lt; Obj &gt;* dependency )
      {
          dependency_ = dependency;
      };
  
  protected:
      Obj GetDependency() const
      {
          return dependency_-&gt;Get();
      };
  
  private:
      IDependency&lt; Obj &gt;* dependency_;
  };
  </code></pre>
  
  <p>Two example test cases. One requires a <code>const wchar_t</code> object; one produces that object:</p>
  
  <pre><code>/// A test case that provides a "const wchar_t*" object to other test cases
  class Foo : public ITest, 
              public IDependency&lt; const wchar_t* &gt;
  {
  public:
      const wchar_t* Get() 
      { 
          if( object_.length() == 0 )
              Execute();
          return object_.c_str();
      };
  
      virtual void Execute()
      {
          printf( "Execute Foo\n" );
          object_ = L"Object produced by Foo";
      };
  
  private:
      std::wstring object_;
  };
  
  /// A test case that first requires a "const wchar_t*" object
  class Bar : public ITest,
              public IDependent&lt; const wchar_t* &gt;
  {
  public:
  
      virtual void Execute()
      {
          const wchar_t* needed_object = GetDependency();
  
          printf( "Execute Bar with %S\n", needed_object );
      };
  };
  </code></pre>
  
  <p>The test cases are stored in a list. Cases are added to the list by a registration process:</p>
  
  <pre><code>/// List of test cases to execute
  std::vector&lt; ITest* &gt; list_;
  
  /// Register a test case to execute with the system
  void Register( ITest* test_case )
  {
      list_.push_back( test_case );
  }
  </code></pre>
  
  <p>Here's my problem. I wanted to implement an overload of the 'Register()' function that also accepts dependencies. But, because dependencies can be of any type (not just the 'const wchar_t*' from this example), I'm not sure how to manage this. Below is an example of more or less what I'm looking for, but I'm not sure how to make it work.</p>
  
  <pre><code>/// Register a test case with dependencies with the system
  void Register( ITest* test_case, ITest* dependency, ... )
  {
      IDependent&lt; ??? &gt;* dependent = dynamic_cast&lt; IDependent&lt; ??? &gt;* &gt;( test_case );
      IDependency&lt; ??? &gt;* dep = dynamic_cast&lt; IDependency&lt; ??? &gt;* &gt;( dependency );
  
      va_list dep_list;
      for( va_start( dep_list, dependency ); 
           NULL != dep; 
           dep = dynamic_cast&lt; IDependency&lt; ??? &gt;* &gt;( va_arg( dep_list, ITest* ) ) )
      {
          dependent-&gt;SetDependency( dep );
      }
      va_end( dep_list );
  
      Register( test_case );
  }
  </code></pre>
  
  <p>An example usage:</p>
  
  <pre><code>int _tmain( int argc, _TCHAR* argv[] )
  {
      /// Test case Foo
      Foo foo;
  
      /// Test case bar (depends on Foo)
      Bar bar;
  
      /// Register test case Bar with a dependency on Foo
      Register( &amp;bar, &amp;foo );
  
      /// Execute Bar. Because it depends on Foo, that will be executed first
      list_-&gt;begin()-&gt;Execute();
      return 0;
  }
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>Execute Foo
  Execute Bar with Object produced by Foo
  </code></pre>
  
  <p>Does anybody have any suggestions on how I can successfully implement this architecture? (or a better architecture that actually works?)</p>
  
  <p>Thanks,
  PaulH</p>