473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

template object as a template parameter ?

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
2 2728
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Istvan Buki | last post by:
Dear C++ gurus, I'm having some problems inserting some template classes into containers. I'm looking for ideas, suggestions,... on how to achieve this but first let me explain my problem....
6
by: Brian Ross | last post by:
Hi, I am trying to do something similar to the following: int Func1(int x, int y); double Func2(double x, double y); template <typename FuncT> // <- What would go here class CObjectT {
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
1
by: sebastian | last post by:
Hi, I'd like to specialize a template function that contains a template parameter. In Example i have the following function declared: .... template < int i > static stupid_object&...
5
by: krishnaroskin | last post by:
Hey all, I've been running into a problem with default values to template'd functions. I've boiled down my problem to this simple example code: #include<iostream> using namespace std; //...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
8
by: Belebele | last post by:
Suppose that I want to write a (concrete) interface class (without virtual functions) to classes that contain a method template // -------------------------------------- class Interface {...
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.