473,287 Members | 3,319 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,287 software developers and data experts.

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<typename 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<typename T,typename R>
class c_reg
{
virtual R get_data(T p_data);
virtual void set_data(T , R);
}

template<typename 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 "declaration 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<typename T>
class c_reg_init : public c_my_reg
{
void set_data (int, std::string)
}

template<typename 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 8583
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<typename 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<typename T,typename R>
class c_reg
{
virtual R get_data(T p_data);
virtual void set_data(T , R);
}

template<typename 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 "declaration 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<typename T>
class c_reg_init : public c_my_reg
{
void set_data (int, std::string)
}

template<typename 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<typename 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<typename 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>::set_data " << p_key << ":" << p_data <<
std::endl;
}
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg.cpp
//c_reg_init.h
/////////////////////////////////////////////////////////////////////////////////////
#include "c_reg.h"

#include <iostream>

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

template<typename 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_data(i, s);
}
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg_init.h
//main.cpp
/////////////////////////////////////////////////////////////////////////////////////
#include "c_reg_init.h"

int main(int, char**)
{

c_reg_init<longmy_c_reg_init;

my_c_reg_init.set_data(3, "4");

c_my_reg * pc_my_reg = &my_c_reg_init;
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<typename 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<typename 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<typename 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>::set_data " << p_key << ":" << p_data <<
std::endl;
}
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg.cpp
//c_reg_init.h
/////////////////////////////////////////////////////////////////////////////////////
#include "c_reg.h"

#include <iostream>

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

template<typename 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_data(i, s);
}
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg_init.h
//main.cpp
/////////////////////////////////////////////////////////////////////////////////////
#include "c_reg_init.h"

int main(int, char**)
{

c_reg_init<longmy_c_reg_init;

my_c_reg_init.set_data(3, "4");

c_my_reg * pc_my_reg = &my_c_reg_init;
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<typename 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
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...
6
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...
4
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...
14
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...
3
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...
23
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? ...
7
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
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...
24
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...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.