tags:

views:

69

answers:

2
template <typename T>
class A
{
public:
   T t;
   void Do (void)
   {
      t.doSomething();
   }
};

In the above, how do I supply parameters to the constructor of 't' at the time of template instantiation?.

Like, if I had:

class P1
{
public:
    P1 (int i, int a) {}
};

class P2
{
public:
    P2 (int a) {}
};

I would like to use both P1 and P2 as template parameters to A.. Currently i'm supplying an instance of P1/P2 to the ctor of A, from where 't' is initialized using a copy-ctor.

The bigger picture (For UncleBens, GMan):

I have a whole lot of data-structures (DS) with a lot of fiends in it. Each of these DS is stored in a Database, shown in the ui and transacted over RPC. I have a class to validate each of these DS. The validation class should be behave differently based on the situation where it is validating. When validation of data fetched from the DB fails, it should log a 'developer understandable' error and die with an assertion. When validation of data got from an rpc-client fails, the server should respond an appropriate error. When validation of data got from an rpc-server fails, it should be logged and the client should crash. When validation fails on the UI, the user should be notified.

I decided to make the 'error-handling' a policy that can be chosen at compile time as a template parameter. However, each of these error-handling mechanism require different ways of construction. And, thats where I'm stuck.

As of now, I have a copy constructor based mechanism where I am mentioning the type twice (once as a parameter to the template, and again as in instance to the ctor of the instantiation), which is redundant.

I want to know how other people would solve such a case.

+2  A: 
template <typename T>
class A
{
public:
   T t;
   void Do (void)
   {
      t.doSomething();
   }
   A() : t(parameters) {};  //Like this
}

In response to edited question:
If you have a variable number of arguments to the templated classes, then you need to wait for C++0x varadic templates.

Billy ONeal
billy: at the time of template instantiation.. I guess i should update my question mentioning this.
Joe Steeve
@Joe Steeve: You have t as a member of the class. t is constructed when the class is constructed. If you want to construct t when the function is called make t a member of the function and construct it there.
Billy ONeal
@billy: I have a lot of functions in 'A'. A uses functions in 'T'. T basically defines a policy..
Joe Steeve
@Joe Steeve: Then you need to make them template functions, not a template class. C++ has free functions for a reason. This is one.
Billy ONeal
@billy: as in, collect the arguments in the ctor of A and pass it on to the ctor of 't' using varadic templates?
Joe Steeve
@Joe Steeve: Yes. The syntax would match Vlad's answer.
Billy ONeal
A: 
template <typename T, int i, int a>
class A
{
public:
   T t;
   A() : t(i, a)
   {
   }
   void Do (void)
   {
      t.doSomething();
   }
}

A<MyT, 5, 8> a;

This works for fixed number of arguments, of course.


Edit:
Without C++0x, you would perhaps need to supply several templates for different number of arguments.


Edit:
If your type is not always int, you can go this way:

template <typename T, typename ARG1, ARG VAL1>
class A
{
    T t;

  public:
    A() : t(VAL1)
    {
    }
};
Vlad
+1 in 47 minutes. Note that the number of parameters of the T template inside A must be known.
Billy ONeal
@vlad: can you give a simple example?
Joe Steeve
@Joe: In C++0x, you could have `template <typename T, int ... i>` and `A() : t(i...)`. However, this is a strange answer. For a tiny bit of syntactic sugar, it doesn't concern you that each A is a different type??
UncleBens
@vlad: the arguments to the ctor are not always 'ints'.
Joe Steeve
@UncleBens: This is what the OP asked for: supplying constructor arguments at template _instantiation_, not object construction!
Vlad