473,320 Members | 2,177 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,320 software developers and data experts.

YATQ (Yet Another Template Question)

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?
Jul 22 '05 #1
4 1566
Aguilar, James wrote:
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 I include the container in the header?

Jul 22 '05 #2
On Thu, 15 Jul 2004 22:15:50 -0400, Aguilar, James
<jf**@cec.NOBOTSwustl.edu> wrote:
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
Jul 22 '05 #3

"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsa7opsdr212331@andronicus...
On Thu, 15 Jul 2004 22:15:50 -0400, Aguilar, James
<jf**@cec.NOBOTSwustl.edu> wrote:

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.
Jul 22 '05 #4
> Aguilar, James wrote:
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. .... 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?


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
Jul 22 '05 #5

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

Similar topics

9
by: Manlio Perillo | last post by:
Regards. In the standard library there are two modules for command line parsing: optparse and getopt. In the Python Cookbook there is another simple method for parsing, using a docstring. ...
8
by: Michele Simionato | last post by:
I was playing with string.Template in Python 2.4 and I came out with the following recipe: import sys from string import Template def merge(*dictionaries): """Merge from right (i.e. the...
6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
1
by: John | last post by:
Hi, before I gouge my eyes out with a teaspoon, hopefully someone will be able to shed some light on the black art of templates! I've exhausted the search engine, FAQs, groups etc looking for...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
0
by: Anonymous | last post by:
I am writing a template HashTable class. I have got it working, but I want to make the code more generic and more robust. The current code looks something like this: template <class InnerType,...
1
by: Anonymous | last post by:
I am writing a template HashTable class. I have got it working, but I want to make the code more generic and more robust. The current code looks something like this: template <class InnerType,...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.