473,409 Members | 1,970 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,409 software developers and data experts.

Forward declarations, templates, and typedefs

I have something like the following:

template<ENUM X, int I = 0>
class Generic
{
....
};

typedef Generic<Val1> v1_type;
typedef Generic<Val2> v2_type;
typedef Generic<Val2, 1> incompat_v2_type;

I'm going to be making this class fundamental to the nature of a rather
large project (the goal is to start using it everywhere instead of
something that is currently done). I don't want to have to include the
header every time I have to return a value of this type. I want to
forward declare in headers and include in source files.

class v1_type; does not work.

I'm not that great with templates and don't fully grasp the subtleties
of what I am trying to accomplish. Can I forward declare this template
and use it? I would prefer to do so based on the typedefs, not the
actual template.

May 2 '06 #1
7 5951
Noah Roberts wrote:
I have something like the following:

template<ENUM X, int I = 0>
class Generic
{
...
};

typedef Generic<Val1> v1_type;
typedef Generic<Val2> v2_type;
typedef Generic<Val2, 1> incompat_v2_type;

I'm going to be making this class fundamental to the nature of a
rather large project (the goal is to start using it everywhere
instead of something that is currently done). I don't want to have
to include the header every time I have to return a value of this
type. I want to forward declare in headers and include in source
files.

class v1_type; does not work.

I'm not that great with templates and don't fully grasp the subtleties
of what I am trying to accomplish. Can I forward declare this
template and use it? I would prefer to do so based on the typedefs,
not the actual template.


Forward-declaring a template is just like forward-declaring anything
else, you just omit the "{..}" part:

template<ENUM X, int I> class Generic;

(now, I am not sure about the default argument, you might want to give
it a try as well).

As to "using" it, you didn't say what that means.

As to "based on the typedefs", I am not sure what you mean. Please
post more code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 2 '06 #2
Noah Roberts wrote:
I have something like the following:

template<ENUM X, int I = 0>
class Generic
{
...
};

typedef Generic<Val1> v1_type;
typedef Generic<Val2> v2_type;
typedef Generic<Val2, 1> incompat_v2_type;

I'm going to be making this class fundamental to the nature of a rather
large project (the goal is to start using it everywhere instead of
something that is currently done). I don't want to have to include the
header every time I have to return a value of this type. I want to
forward declare in headers and include in source files.

class v1_type; does not work.

I'm not that great with templates and don't fully grasp the subtleties
of what I am trying to accomplish. Can I forward declare this template
and use it? I would prefer to do so based on the typedefs, not the
actual template.


You can't forward-declare a typedef because it is just an alias for
some other type, not a type itself. You can forward declare a class
template as such:

template<class T> class SomeClassTemplate;

But, to forward declare a class template with default parameters, you
have to specify them in the forward declaration:

template<ENUM X, int I = 0> class Generic;

Then you can include perhaps just your typedefs. You'll end up with
something like this:

enum ENUM { A, B };

template<ENUM X, int I = 0> class Generic;

typedef Generic<A> SomeTypeA;
typedef Generic<B> SomeTypeB;
typedef Generic<A,1> SomeTypeBad;

SomeTypeA* pa = 0;
SomeTypeB* pb = 0;
SomeTypeBad* pbad = 0;

template<ENUM X, int I = 0> class Generic
{
public:
enum { val = I };
ENUM foo() const { return X; }
};

Cheers! --M

May 2 '06 #3

mlimber wrote:
I'm not that great with templates and don't fully grasp the subtleties
of what I am trying to accomplish. Can I forward declare this template
and use it? I would prefer to do so based on the typedefs, not the
actual template.


You can't forward-declare a typedef because it is just an alias for
some other type, not a type itself. You can forward declare a class
template as such:

template<class T> class SomeClassTemplate;

But, to forward declare a class template with default parameters, you
have to specify them in the forward declaration:

template<ENUM X, int I = 0> class Generic;

Then you can include perhaps just your typedefs. You'll end up with
something like this:

enum ENUM { A, B };

template<ENUM X, int I = 0> class Generic;

typedef Generic<A> SomeTypeA;
typedef Generic<B> SomeTypeB;
typedef Generic<A,1> SomeTypeBad;

SomeTypeA* pa = 0;
SomeTypeB* pb = 0;
SomeTypeBad* pbad = 0;

template<ENUM X, int I = 0> class Generic
{
public:
enum { val = I };
ENUM foo() const { return X; }
};


So actually it might be best to create an "interface" header that had
the forward declaration and the typedefs and include it in the headers
of the clients. Then include the actual definition in sources.

I like templates but getting rid of the compile time dependencies with
them can be tough...

May 2 '06 #4
Noah Roberts wrote:
mlimber wrote:
I'm not that great with templates and don't fully grasp the subtleties
of what I am trying to accomplish. Can I forward declare this template
and use it? I would prefer to do so based on the typedefs, not the
actual template.
You can't forward-declare a typedef because it is just an alias for
some other type, not a type itself. You can forward declare a class
template as such:

template<class T> class SomeClassTemplate;

But, to forward declare a class template with default parameters, you
have to specify them in the forward declaration:

template<ENUM X, int I = 0> class Generic;

Then you can include perhaps just your typedefs. You'll end up with
something like this:

enum ENUM { A, B };

template<ENUM X, int I = 0> class Generic;

typedef Generic<A> SomeTypeA;
typedef Generic<B> SomeTypeB;
typedef Generic<A,1> SomeTypeBad;

SomeTypeA* pa = 0;
SomeTypeB* pb = 0;
SomeTypeBad* pbad = 0;

template<ENUM X, int I = 0> class Generic
{
public:
enum { val = I };
ENUM foo() const { return X; }
};


So actually it might be best to create an "interface" header that had
the forward declaration and the typedefs and include it in the headers
of the clients. Then include the actual definition in sources.


If the forward declaration and typedefs are used widely, then sure.
I like templates but getting rid of the compile time dependencies with
them can be tough...


Sometimes it's impossible. For instance, you can't (portably)
forward-declare STL containers such as std::vector since
implementations are permitted to attach as many extra template
parameters as they like, so long as they're given defaults.

Cheers! --M

May 2 '06 #5
mlimber wrote:
Noah Roberts wrote:
I like templates but getting rid of the compile time dependencies with
them can be tough...


Sometimes it's impossible. For instance, you can't (portably)
forward-declare STL containers such as std::vector since
implementations are permitted to attach as many extra template
parameters as they like, so long as they're given defaults.


Last I heard, that's wrong. See http://tinyurl.com/oestb.
Jonathan

May 2 '06 #6
Jonathan Mcdougall wrote:
mlimber wrote:
Noah Roberts wrote:
I like templates but getting rid of the compile time dependencies with
them can be tough...


Sometimes it's impossible. For instance, you can't (portably)
forward-declare STL containers such as std::vector since
implementations are permitted to attach as many extra template
parameters as they like, so long as they're given defaults.


Last I heard, that's wrong. See http://tinyurl.com/oestb.


I stand corrected!

Cheers! --M

May 2 '06 #7

"mlimber" <ml*****@gmail.com> skrev i meddelandet
news:11*********************@g10g2000cwb.googlegro ups.com...
Jonathan Mcdougall wrote:
mlimber wrote:
> Noah Roberts wrote:
> > I like templates but getting rid of the compile time
> > dependencies with
> > them can be tough...
>
> Sometimes it's impossible. For instance, you can't (portably)
> forward-declare STL containers such as std::vector since
> implementations are permitted to attach as many extra template
> parameters as they like, so long as they're given defaults.


Last I heard, that's wrong. See http://tinyurl.com/oestb.


I stand corrected!

Cheers! --M


The real reason you cannot forward declare std::vector is that it has
default template parameters. For some reason, the language doesn't let
you repeat the defaults in both a template definition and a
declaration. So, as long as the std::vector definition contains those
defaults, you cannot write a valid declaration for it.

Note that iosfwd solves this problem for iostreams, but not for any
other std::classes.
Bo Persson
May 2 '06 #8

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

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
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...
5
by: John Gabriele | last post by:
I'm hoping someone can please help me remember the C++ rule: When you're writing a header file for a class (say, some_namespace::Bar), and that class makes use of another class...
11
by: aleko | last post by:
This applies equally to function prototypes. Why do we have to tell the compiler twice? Other modern compiled languages don't have this requirement. Just curious, Aleko
9
by: vishnu | last post by:
what is the exact difference between including a class header file and forward declaration. and Is there a case , where in forward declaration is not possible and including is .
2
by: Neo | last post by:
Hi, I am new to C++ and want to know what are forward declarations and any site which has a good introductory explanation. thanks in advance, nick
12
by: fox | last post by:
How do I (portably) make a forward reference to a static (I mean file-scope) variable? I've been using "extern" for years, for example: extern int x; int foo(void) { return x++; }
2
by: Carlos Martinez Garcia | last post by:
Hi all: I usually make forward declarations in headers. Something like this: class MyClass; Now, I need a reference to a type defined like this (traditional C Style): typedef struct {
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.