By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

template object as a template parameter ?

P: n/a
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.
Nov 3 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Siegfried Weiss wrote:
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.

Nov 3 '05 #2

P: n/a
Rolf Magnus wrote:
Siegfried Weiss wrote:

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.


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
Nov 3 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.