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

Question about Item 23 of C++ coding standard

The title of these Item is "Make header files self-sufficient"
There're two examples in this Item:

Example 1: Dependent names. Templates are compiled at the point where
they are defined, except that any dependent names or types are not
compiled until the point where the template is instantiated. This means
that a template<class Tclass Widget with a std::deque<Tmember does
not incur a compile-time error even when <dequeis not included, as
long as nobody instantiates Widget. Given that Widget exists in order
to be instantiated, its header clearly should #include <deque>.

Example 2: Member function templates, and member functions of
templates, are instantiated only if used. Suppose that Widget doesn't
have a member of type std::deque<T>, but Widget's transmogrify member
function uses a deque. Then Widget's callers can instantiate and use
Widget just fine even if no one includes <deque>, as long as they don't
use TRansmogrify. By default, the Widget header should still #include
<dequebecause it is necessary for at least some callers of Widget. In
rare cases where an expensive header is being included for few rarely
used functions of a template, consider refactoring those functions as
nonmembers supplied in a separate header that does include the
expensive one.

I wrote examples according it:

*********example1:
//widget.h
#include <iostream>
#include <deque //without this, complie error
generated.

template<typename Tclass Widget {
std::deque<Tfoo_;
int type_;
public:
void printWidget() {std::cout << "Widget\n";}
};

//widget.c
#include "widget.h"

int main(void) {
return 0;
}

*********example2:

//widget.h
#include <iostream>
#include <deque>
#include <vector //without this, complie error generated.

template<typename Tclass Widget {
std::deque<Tfoo_;
int type_;
public:
void printWidget() {std::cout << "Widget\n";}
void useVector() {std::vector<Tv;}
};

//widget.c
#include "widget.h"

int main(void) {
Widget<intwgt;
wgt.printWidget();
return 0;
}

I compile it using g++ under Linux. Why the result is different with
what the book says?
Can anyone give some advice?

Jul 28 '06 #1
2 1257
sharpblade wrote:
[...]
I compile it using g++ under Linux. Why the result is different with
what the book says?
I just tried your code with gcc 4.1.0-3, and no error was generated.
Can anyone give some advice?
Upgrade your compiler.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '06 #2
sharpblade wrote:
The title of these Item is "Make header files self-sufficient"
Did they point out the best technique to make that super easy? Given a
module (group of classes) called Foo, declare them in Foo.h, and define them
in Foo.cpp.

The first line of Foo.cpp is this:

#include "Foo.h"

Because there's nothing above the #include line, Foo.h must be capable of
compiling all its declarations. The best way uses forward declarations, like
this:

class Bar;

class Foo
{
...
Bar & aBar;
};

The next best way nests more #include files inside Foo.h. But sometimes we
must do that.
There're two examples in this Item:

Example 1: Dependent names. Templates are compiled at the point where
they are defined, except that any dependent names or types are not
compiled until the point where the template is instantiated. This means
that a template<class Tclass Widget with a std::deque<Tmember does
not incur a compile-time error even when <dequeis not included, as
long as nobody instantiates Widget. Given that Widget exists in order
to be instantiated, its header clearly should #include <deque>.
Without #include <dequeinside Foo.h, can my Foo.cpp start with this?

#include "Foo.h"
#include <deque>

If it can, then it breaks my guideline that we can detect Foo.h's
self-sufficiency by compiling it first. So move #include <dequeinside
Foo.h anyway...
Example 2: Member function templates, and member functions of
templates, are instantiated only if used. Suppose that Widget doesn't
have a member of type std::deque<T>, but Widget's transmogrify member
function uses a deque. Then Widget's callers can instantiate and use
Widget just fine even if no one includes <deque>, as long as they don't
use TRansmogrify. By default, the Widget header should still #include
<dequebecause it is necessary for at least some callers of Widget. In
rare cases where an expensive header is being included for few rarely
used functions of a template, consider refactoring those functions as
nonmembers supplied in a separate header that does include the
expensive one.
Again, can users of Foo.h include <dequeon the next line?
void printWidget() {std::cout << "Widget\n";}
void useVector() {std::vector<Tv;}
These are member functions, not member function templates. Make them
templates, based on either T or something else, and see what happens.
I compile it using g++ under Linux. Why the result is different with
what the book says?
Because the authors spend too much time reading the Standard. ;-)

No compiler exactly matches the Standard. So note that Herb & Alex's advice
devolves to what we must do with less-than-Standard compilers. #include
<dequeinside our widget.h. If <dequewere instead something heavy,
refactor _everything_ to put the deque into a delegated object.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 28 '06 #3

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

Similar topics

19
by: Anon Email | last post by:
Hi everyone, Let's see, now. This question is about the capabilities of ANSI C++. I want to write and compile code in ANSI C++ that, when compiled, will make the computer speaker beep; or, at...
10
by: Jason | last post by:
Hi, I have a few million data items being received by my program and I wish to store them in a list, with each item being inserted in any position in the list. Any performance tips so that my...
55
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
13
by: Don Miller | last post by:
Here's what I want to do... sorry for the lengthy description ;-) I have an inventory tracking system with items that I track stock levels on. Some items are counted with decimal precision...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
25
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking....
10
by: > Adrian | last post by:
I am getting these errors: DrawListViewItemEventArgs' could not be found DrawListViewSubItemEventArgs' could not be found DrawListViewColumnHeaderEventArgs' could not be found I am already...
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
1
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.