template object as a template parameter ?

  • Thread starter Siegfried Weiss
  • Start date
S

Siegfried Weiss

Hi guys,

i give up finding a solution by reading or by trial & error. Hope, YOU
can help me! (Sorry for my rather long posting.)

Stroustrup says, that templates could be declared with
- type parameter, e.g. template<class T>
- parameters with *simple* types like int, e.g. template<int size>
- template parameter, e.g. template< template<class> class A >
the latter beeing a type specifier, not an instance of A.

So, there is no way to declare a template class with an *object* of my
own (template) class A ??


I want to declare a template class giving it a template object as a
template parameter:


// an instance of such a class should be a template parameter

template <class T>
class A {
public:
typedef T data_type_A;
...
};


// the template, which gets an object of type A<> as a template parameter
// the syntax is (not yet) correct (that exactly is my problem)

template <template<class> A theObjectOfA> // <<< the problem
class B {
public:

// an obj of A, type <T> doesn't matter
const template<class> A &mA;

// the type <T> of A is used only here
typedef typename theObjectOfA::data_type_A data_type_B;

B() : mA( theObjectOfA) {}

...
};


In the above line in question, i want to express, that

- theObjectOfA is a parameter for the template - not beeing
of a *simple* type line an int - and
- theObjectOfA could be of ANY type T, so T should not be specified
here. Later, when i use this class, the compiler knows the Type T
of the given object.

How can i achieve this ?


The reason, why i want to accomplish this is: there should be defined
MANY (static) objects of derived B's, each will be given a (static)
object of A as parameter:

class C : public B<obj_1_OfA> {
public:
C() : B<obj_1_OfA>() {}
};



The class C must have in common the datatype T of objOfA. Because the
compiler knows the class T of A, why shouldn't i use his knowledge.


I could solve the problem, if B would be declared like:


template <class T>
class B {
public:
typedef T data_type_B;

const A<T> mA;

B(const A<T> &theObjectOfA) : mA( theObjectOfA ) {}

...
};


Then, C must be written as:

class C : public B<anyClassT> {
public:
C() : B<anyClassT> (anObjectOfA_forClassT) {}
};

But then I must care to use the same type T for C and the argument for
B::B().


Thx for any advice.

Siggi.
 
R

Rolf Magnus

Siegfried said:
Hi guys,

i give up finding a solution by reading or by trial & error. Hope, YOU
can help me! (Sorry for my rather long posting.)

Stroustrup says, that templates could be declared with
- type parameter, e.g. template<class T>
- parameters with *simple* types like int, e.g. template<int size>
- template parameter, e.g. template< template<class> class A >
the latter beeing a type specifier, not an instance of A.

So, there is no way to declare a template class with an *object* of my
own (template) class A ??

No, and it doesn't matter whether A is a template or not. You can't use
class instances as template arguments.
 
V

Victor Bazarov

Rolf said:
Siegfried Weiss wrote:




No, and it doesn't matter whether A is a template or not. You can't use
class instances as template arguments.

It is still allowed to declare a template class with a *pointer* or
a *reference* to an object, if the object has external linkage, isn't it?

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top