473,606 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help on correct usage of forward declaration of templates

Hi

I have a question on usage of forward declarations of templates. While
searching through this group , people suggested using forward
declarations and typedefs for templates as

// in myfile.h
template<typena me T,typename R>
class some_class;

typedef some_class<int, floatmy_class;

then use my_class as an instance in the program. The advantage in this
is obviously i dont have to include the header file declaring
some_class. This would help in recompilation dependencies.

Now i tried out the same as shown below. Note: i have mentioned the
errors in comments
//c_reg.h
template<typena me T,typename R>
class c_reg
{
virtual R get_data(T p_data);
virtual void set_data(T , R);
}

template<typena me T,typename RR c_reg::get_data (T p_data)
{
... //do something
}

template <typename T,typename Rvoid c_reg::set_data (T p_key, R
p_data)
{
... //do something
}
//end of c_reg.h

------------------------------------------------------------------------------------
//c_reg_init.h

// below forward declaration gives error
// error is "declaratio n of struct c_my_reg"
template<T,R>
class c_reg;
typedef c_reg<int,std:: stringc_my_reg;

// also gives error here saying
// invlaid use of undefined type c_my_reg
template<typena me T>
class c_reg_init : public c_my_reg
{
void set_data (int, std::string)
}

template<typena me Tvoid c_reg_init<T>:: set_data(int, std::string)
{
...//do something
}
//end of c_reg_init.h

Now as seen above, i'm tyring to avoid inclusion of c_reg.h in
c_reg_init header file. But using the forward declaration syntax gives
errror mentioned above. It assumes the forward declared template class
c_reg to be a struct i dont know why.

What i want to basically do is hide the template header file so that i
can give the user a interface. In this interface i should be able to
just forward declare the template class
Is there a way around?

Jan 8 '07 #1
6 8610
Hunk napsal:
Hi

I have a question on usage of forward declarations of templates. While
searching through this group , people suggested using forward
declarations and typedefs for templates as

// in myfile.h
template<typena me T,typename R>
class some_class;

typedef some_class<int, floatmy_class;

then use my_class as an instance in the program. The advantage in this
is obviously i dont have to include the header file declaring
some_class. This would help in recompilation dependencies.
Well, you do not need it, if you want to declare pointer or reference
to my_class. But if you need to access any method (or attribute) of
this class, you need the definition of this class as well as in case
you need to declare instance of this class.

It is the same as in following example without templates:
class X;

X& rx; // OK
X* px; // OK
X x; // Error

void X::method() // Error - nothing is known about methods of class X
{
}
Now i tried out the same as shown below. Note: i have mentioned the
errors in comments
//c_reg.h
template<typena me T,typename R>
class c_reg
{
virtual R get_data(T p_data);
virtual void set_data(T , R);
}

template<typena me T,typename RR c_reg::get_data (T p_data)
{
... //do something
}

template <typename T,typename Rvoid c_reg::set_data (T p_key, R
p_data)
{
... //do something
}
//end of c_reg.h

------------------------------------------------------------------------------------
//c_reg_init.h

// below forward declaration gives error
// error is "declaratio n of struct c_my_reg"
template<T,R>
class c_reg;
typedef c_reg<int,std:: stringc_my_reg;

// also gives error here saying
// invlaid use of undefined type c_my_reg
template<typena me T>
class c_reg_init : public c_my_reg
{
void set_data (int, std::string)
}

template<typena me Tvoid c_reg_init<T>:: set_data(int, std::string)
{
...//do something
}
//end of c_reg_init.h

Now as seen above, i'm tyring to avoid inclusion of c_reg.h in
c_reg_init header file. But using the forward declaration syntax gives
errror mentioned above. It assumes the forward declared template class
c_reg to be a struct i dont know why.

What i want to basically do is hide the template header file so that i
can give the user a interface. In this interface i should be able to
just forward declare the template class
Is there a way around?
If you need generic class, you have to give to user whole
implementation. You can split it into 2 headers, one contains only
declaration of templated class and second contains full implementation,
so you can speed up compilation simillary as with #include <iosfwdand
#include <iostream>

Jan 8 '07 #2

Ondra Holub wrote:
Hunk napsal:
Well, you do not need it, if you want to declare pointer or reference
to my_class. But if you need to access any method (or attribute) of
this class, you need the definition of this class as well as in case
you need to declare instance of this class.

It is the same as in following example without templates:
class X;

X& rx; // OK
X* px; // OK
X x; // Error

void X::method() // Error - nothing is known about methods of class X
{
}

If you need generic class, you have to give to user whole
implementation. You can split it into 2 headers, one contains only
declaration of templated class and second contains full implementation,
so you can speed up compilation simillary as with #include <iosfwdand
#include <iostream>
So there's no way other than to include the header file which contains
the declaration?
That means I would have to necessarily include the declaration header
file , and using forward declaration has no effect except for use as
pointers. The methods cannot be accessed for a template class? Is there
any method people have to hide the template declarations?

Jan 9 '07 #3

Hunk wrote:
Ondra Holub wrote:
Hunk napsal:
Well, you do not need it, if you want to declare pointer or reference
to my_class. But if you need to access any method (or attribute) of
this class, you need the definition of this class as well as in case
you need to declare instance of this class.

It is the same as in following example without templates:
class X;

X& rx; // OK
X* px; // OK
X x; // Error

void X::method() // Error - nothing is known about methods of class X
{
}

If you need generic class, you have to give to user whole
implementation. You can split it into 2 headers, one contains only
declaration of templated class and second contains full implementation,
so you can speed up compilation simillary as with #include <iosfwdand
#include <iostream>

So there's no way other than to include the header file which contains
the declaration?
That means I would have to necessarily include the declaration header
file , and using forward declaration has no effect except for use as
pointers. The methods cannot be accessed for a template class? Is there
any method people have to hide the template declarations?
Template class forward declarations are normally in a source file, and
not placed in a header.
If done properly, you can put your template source code in the *.cpp
source file.
Template class forward declaration method limits the available types
that can be used with the template. From looking at your code, this
seems to be acceptable for your requirements.

Here's an example usage:

//c_reg.h
/////////////////////////////////////////////////////////////////////////////////////
#include <string>

template<typena me T,typename R>
class c_reg
{
public:
virtual R get_data(T p_data);
virtual void set_data(T , R);
};

typedef c_reg<int,std:: stringc_my_reg; //This is NOT a forward
template declaration
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg.h

/c_reg.cpp
/////////////////////////////////////////////////////////////////////////////////////
#include <iostream>

#include "c_reg.h"

//*************** **************
template c_reg<int,std:: string>; //This IS a template class forward
declaration!!!
//*************** **************

template<typena me T,typename RR c_reg<T,R>::get _data(T p_data)
{
return R();
}

template <typename T,typename Rvoid c_reg<T,R>::set _data(T p_key, R
p_data)
{
std::cout << "c_reg<T,R>::se t_data " << p_key << ":" << p_data <<
std::endl;
}
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg.cpp
//c_reg_init.h
/////////////////////////////////////////////////////////////////////////////////////
#include "c_reg.h"

#include <iostream>

template<typena me T>
class c_reg_init : public c_my_reg
{
public:
void set_data (int, std::string);
};

template<typena me Tvoid c_reg_init<T>:: set_data(int i, std::string s)

{
std::cout << "c_reg_init<T>: :set_data " << i << ":" << s <<
std::endl;
c_my_reg::set_d ata(i, s);
}
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg_init.h
//main.cpp
/////////////////////////////////////////////////////////////////////////////////////
#include "c_reg_init .h"

int main(int, char**)
{

c_reg_init<long my_c_reg_init;

my_c_reg_init.s et_data(3, "4");

c_my_reg * pc_my_reg = &my_c_reg_in it;
pc_my_reg->set_data(5, "6");

return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
//end of main.cpp

Jan 9 '07 #4

Hunk wrote:
Hi

I have a question on usage of forward declarations of templates. While
searching through this group , people suggested using forward
declarations and typedefs for templates as

// in myfile.h
template<typena me T,typename R>
class some_class;

typedef some_class<int, floatmy_class;

then use my_class as an instance in the program. The advantage in this
You cannot use forward declaration for base class and it as nothing to
do with templates. If you have class B that class D derived from it you
must include class B H file in class D H file since the complier needs
to know the full declaration of the base class for the derived class.
This is true also for members in the class that are not pointers or
reference and for any function that accept by value or return by value.
Again it has nothing to do with templates

Jan 9 '07 #5
>
Template class forward declarations are normally in a source file, and
not placed in a header.
If done properly, you can put your template source code in the *.cpp
source file.
Template class forward declaration method limits the available types
that can be used with the template. From looking at your code, this
seems to be acceptable for your requirements.

Here's an example usage:

//c_reg.h
/////////////////////////////////////////////////////////////////////////////////////
#include <string>

template<typena me T,typename R>
class c_reg
{
public:
virtual R get_data(T p_data);
virtual void set_data(T , R);
};

typedef c_reg<int,std:: stringc_my_reg; //This is NOT a forward
template declaration
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg.h

/c_reg.cpp
/////////////////////////////////////////////////////////////////////////////////////
#include <iostream>

#include "c_reg.h"

//*************** **************
template c_reg<int,std:: string>; //This IS a template class forward
declaration!!!
//*************** **************

template<typena me T,typename RR c_reg<T,R>::get _data(T p_data)
{
return R();
}

template <typename T,typename Rvoid c_reg<T,R>::set _data(T p_key, R
p_data)
{
std::cout << "c_reg<T,R>::se t_data " << p_key << ":" << p_data <<
std::endl;
}
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg.cpp
//c_reg_init.h
/////////////////////////////////////////////////////////////////////////////////////
#include "c_reg.h"

#include <iostream>

template<typena me T>
class c_reg_init : public c_my_reg
{
public:
void set_data (int, std::string);
};

template<typena me Tvoid c_reg_init<T>:: set_data(int i, std::string s)

{
std::cout << "c_reg_init<T>: :set_data " << i << ":" << s <<
std::endl;
c_my_reg::set_d ata(i, s);
}
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg_init.h
//main.cpp
/////////////////////////////////////////////////////////////////////////////////////
#include "c_reg_init .h"

int main(int, char**)
{

c_reg_init<long my_c_reg_init;

my_c_reg_init.s et_data(3, "4");

c_my_reg * pc_my_reg = &my_c_reg_in it;
pc_my_reg->set_data(5, "6");

return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
//end of main.cpp
Axter, thanks for your response. I have a working model of what you
mentioned. The thing i was looking forward to is a way of not including
the header file , i.e in the example," c_reg.h" in c_reg_init.h.
Basically there is a group of template files and i need to provide an
interface mechanism to the user to use my class. My objective is not to
include the header file so as to avoid compilation dependency.I was
looking at a way to forward declare the template classes instead of
including the header file. Forward declare c_reg and use it in the
typedef's to do further things. But i guess this is not possible, for
the simple reason that if no file includes the header file then it
would not get the code to generate it at all.. so i thing what i'm
hoping for is akin to a prepetual m/c

Jan 9 '07 #6

bo*******@gmail .com wrote:
Hunk wrote:
Hi

I have a question on usage of forward declarations of templates. While
searching through this group , people suggested using forward
declarations and typedefs for templates as

// in myfile.h
template<typena me T,typename R>
class some_class;

typedef some_class<int, floatmy_class;

then use my_class as an instance in the program. The advantage in this
You cannot use forward declaration for base class and it as nothing to
do with templates. If you have class B that class D derived from it you
must include class B H file in class D H file since the complier needs
to know the full declaration of the base class for the derived class.
This is true also for members in the class that are not pointers or
reference and for any function that accept by value or return by value.
Again it has nothing to do with templates
Yep got your point here. Working on having the forward declarations in
a manager interface and then having the manager include these header
files. The manager interface would be given to the user which will
essentially implement a singleton. I think this should solve the problem

Jan 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8952
by: Oplec | last post by:
Hi, I am trying to figure out the correct syntax for declaring a friend function in a class template and then defining that member after the class. The code example below is what I am trying to get to work properly; it generates a link error. Thank you, Oplec. #include <cstdlib>
6
5197
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define TESTDRIVER_HPP #include <ostream> namespace ns_testdriver{ using std::ostream;
4
2774
by: Piotr Sawuk | last post by:
I'm a newbie in the world of c++, and I am used to learn a programming language simply by programming. Unfortunately I where unable to find any useful helpfile for this language, in which such basic things as "class", "operator ::", or even my compiler's error-messages would get explained in a quick-reference kind of way (as turbo pascal did with the pascal-language). Therefore I was forced to read some books and tutorials. However, I'm...
14
2146
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type too: using std::vector<int>; Both seem to "work" on the compiler I have and I can't find any documentation saying which is correct, or are both correct?
3
2726
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a template class called "myTemplate", for which there is an instantiation for 'int' type. Now suppose a class in module B binary wants to use this template
23
3833
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
7
5980
by: Noah Roberts | last post by:
I have something like the following: template<ENUM X, int I = 0> class Generic { .... }; typedef Generic<Val1> v1_type; typedef Generic<Val2> v2_type;
0
1467
by: Rune Allnor | last post by:
Hi all. I have these classes, implemented as templates in header files. Right now I have one file per class, where both the declarations and implementations of one class are located in each file. I would like to apply the visitor pattern to some of these classes. One of the technicalities behind the visitor pattern is that one needs forward declarations of classes, since there are two class hierarchies which refer to each other.
24
2558
by: Joe, G.I. | last post by:
Can anyone help me w/ a priority_queue. I'm generating MyEvent classes and I put them on a priority_queue, but I don't know how to get them in priority. The priority is the event w/ the smallest timestamp. // just a wrapper around priority_queue pq = new MyPriorityQueue(); // I generate 10 events w/ a random timestamp for (int i = 0; i < 10; i++) { MyEvent *event = new MyEvent();
0
8016
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7955
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8440
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8306
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5466
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1557
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.