473,387 Members | 1,504 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,387 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 8590
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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...

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.