S
sharpblade
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 T> class Widget with a std::deque<T> member does
not incur a compile-time error even when <deque> is 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
<deque> because 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 T> class Widget {
std::deque<T> foo_;
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 T> class Widget {
std::deque<T> foo_;
int type_;
public:
void printWidget() {std::cout << "Widget\n";}
void useVector() {std::vector<T> v;}
};
//widget.c
#include "widget.h"
int main(void) {
Widget<int> wgt;
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?
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 T> class Widget with a std::deque<T> member does
not incur a compile-time error even when <deque> is 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
<deque> because 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 T> class Widget {
std::deque<T> foo_;
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 T> class Widget {
std::deque<T> foo_;
int type_;
public:
void printWidget() {std::cout << "Widget\n";}
void useVector() {std::vector<T> v;}
};
//widget.c
#include "widget.h"
int main(void) {
Widget<int> wgt;
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?