Topic: How are template instances generated?


Author: kohlhaas@faw.uni-ulm.de (Bernhard Kohlhaas)
Date: 24 Jan 1994 08:34:09 GMT
Raw View
Hello,

during my attempt to generate a special purpose pointer substitution class
I have run into a problem involving the generation of template instances by
the compiler. I remember that a similar question was asked some time ago, but
I can't locate it, so please provide a pointer to the original posting or
provide some insights into solving my problem.

My class looks somewhat like this:

 template <class T> class ptr {
  // ... some private stuff
  public:
   ptr();
   ptr(T*);
   ptr(const ptr<T>&);
   ~ptr();
   operator T*() const;
   T* operator->() const;
   T& operator*() const;
 };

The problem I have is the operator->. I know I need to overload that operator
to resolve access to class members for pointers to a defined class. But when
I defined an instance of a "ptr" to a standard type like "ptr<int>" the compiler
(IBM CSet++ for OS/2) creates an error. It seems like this compiler creates an
instance of every member function of every template instance no matter if that
function is actually used. And since section 13.4.6 of the ARM states that
operator-> MUST be a member function I cannot make it a friend function.

Any idea how this problem can be solved? Is there a well defined behavior on
how a compiler should generate instances of template functions?

One idea I had was to explicitly define instances of operator-> for build-in
types that return a pointer to a dummy class and throw an exception when invoked.
But I am a little hesitant to do that, because it just doesn't appear to be
a "clean" solution to the problem.

Any help would be appreciated.


Bernhard Kohlhaas

--
Bernhard Kohlhaas
-------------------------------------
| Internet: kohlhaas@faw.uni-ulm.de |
|   Bitnet: kohlhaas@dulfaw1a       |
-------------------------------------
#include <StandardDisclaimer>