YATQ (Yet Another Template Question)

A

Aguilar, James

Hey y'all. Suppose I want a member template in a class, but I don't want to
include .hh files in a .hh file? For instance:

#ifndef _FOO_HH_
#define _FOO_HH_ 1

class Foo
{
public:
Foo()
private:
std::priority_queue<int> bar;
}
#endif

This doesn't work. So I tried adding this line above the line of class Foo:

class priority_queue<int>;

Doesn't work. Among other things that don't work are:

template<class T> priority_queue<T>;
priority_queue<int>;
class std::priority_queue<int>;
template<class T> std::priority_queue<T>;
std::priority_queue<int>;

My point is that I want to follow the style guidelines and not include
things in a header file . . . or wait, was it don't "use" namespaces in a
header file? In any case, what is the proper way to get this done? Should
I include the container in the header?
 
G

Gianni Mariani

Hey y'all. Suppose I want a member template in a class, but I don't want to ..... snip
My point is that I want to follow the style guidelines and not include
things in a header file . . . or wait, was it don't "use" namespaces in a
header file?

In this case you have little choice but to add the header for
priority_queue because you're defining a member of a class as that type
of object.

You definitly need to limit "using namespace" in header files. There is
little point in namespaces if everyone uses every namespace in every
header file !

In any case, what is the proper way to get this done? Should
 
J

John Harrison

Hey y'all. Suppose I want a member template in a class, but I don't
want to
include .hh files in a .hh file? For instance:

#ifndef _FOO_HH_
#define _FOO_HH_ 1

class Foo
{
public:
Foo()
private:
std::priority_queue<int> bar;
}
#endif

This doesn't work. So I tried adding this line above the line of class
Foo:

class priority_queue<int>;

Doesn't work. Among other things that don't work are:

template<class T> priority_queue<T>;
priority_queue<int>;
class std::priority_queue<int>;
template<class T> std::priority_queue<T>;
std::priority_queue<int>;

My point is that I want to follow the style guidelines and not include
things in a header file

Since when has that been a style guide? I know that in large projects it
sometimes makes sense to avoid execessive 'includes within includes' to
speed up compilation times and reduce the need for recompilation but even
that is less of an issue on modern faster computers.

Maybe you could check to see if you compiler offers a precompiled headers
option.

. . . or wait, was it don't "use" namespaces in a
header file?

That is defintely to be avoided. The reason being that anyone who uses
your header will also get the use declaration, which could stop there code
from compiling if the happen to be using the same name as you.

In any case, what is the proper way to get this done?
Should
I include the container in the header?

Yes

#include <queue>

There is no other way to get this to work, as far as I can tell.

john
 
A

Aguilar, James

John Harrison said:
On Thu, 15 Jul 2004 22:15:50 -0400, Aguilar, James

Yes

#include <queue>

There is no other way to get this to work, as far as I can tell.

Super. Thanks for the help, I guess that was just my programming blonde
moment of the hour.
 
D

David Fisher

In this case you have little choice but to add the header for
priority_queue because you're defining a member of a class as that type
of object.

Another alternative is to always "#include <queue>" before you "#include
foo.hh" in every source file that uses it (ie. it is possible to never
include any headers inside other headers by requiring dependent headers to
be included first).

Or you could use the "pimpl" idiom (private implementation) and just have a
member in class Foo declared as:

FooImpl *m_impl;

Then you can forward declare FooImpl in foo.hh and no one outside class Foo
needs to know anything about its internals ...

See http://www.gotw.ca/gotw/028.htm for more info on the "pimpl" idiom.

David Fisher
Sydney, Australia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top