Topic: Problem with new in templates...


Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/03/15
Raw View
adamnash@cs.stanford.edu (Adam Nash) writes:

>[...] for semantic reasons, the template assumes that DATA is a pointer type.
[...]
>template <class DATA>
>void GIDTable::ReadFromStream(LStream *inStream)
>{
>   DATA data;
>
>   ...
>
>   data = new DATA;
>   (*data).ReadFromStream(inStream);
>
>   ...
>}
>
>
>The problem occurs on the second line.  data is of type DATA, so you
>really want to allocate a new (*DATA).  IE, if DATA is a CBar *, then data
>is a CBar *,
>so we want it to say: data = new CBar;
>
>How can I do this, only knowing the pointer type?

You can use a partial template specialization:

 template <class T> struct dereference {};
 template <class T> struct dereference <T*> {
  typedef T type;
 };

Then just write

 data = new dereference<Data>::type;

(I note in passing that this would of course be less obfuscated if C++
supported template typedefs...)

Now, the reason that all of this belongs in comp.std.c++ not comp.lang.c++
is that the above code is valid according to the standard, but is highly
unlikely to work with your favourite C++ compiler.  Partial specialization
is a relatively new feature, and few C++ compilers support it yet.

--
Fergus Henderson              WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au               PGP: finger fjh@128.250.37.3
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: tony@online.tmx.com.au (Tony Cook)
Date: 1996/03/15
Raw View
Adam Nash (adamnash@cs.stanford.edu) wrote:
: OK Problem Scenario:

: There is a templatized data structure that is based on type DATA.  This
: data structure features a method that depends on a method in type DATA.
: Thus, for semantic reasons, the template assumes that DATA is a pointer
: type.
:...
: The problem occurs on the second line.  data is of type DATA, so you
: really want to allocate a new (*DATA).  IE, if DATA is a CBar *, then data
: is a CBar *,
: so we want it to say: data = new CBar;

: How can I do this, only knowing the pointer type?
: IE, is there a version of new that can take the pointer type, rather than
: the actual type?  RTTI is fair game.

How about:
 template <class T>
        T* new_of_ptr(T *  /* ignored */)
 {
   return new T;
 }

 ...
 DATA value = new_of_ptr((DATA)0);
 ...

--
        Tony Cook - tony@online.tmx.com.au
                    100237.3425@compuserve.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: adamnash@cs.stanford.edu (Adam Nash)
Date: 1996/03/15
Raw View
OK Problem Scenario:

There is a templatized data structure that is based on type DATA.  This
data structure features a method that depends on a method in type DATA.
Thus, for semantic reasons, the template assumes that DATA is a pointer
type.

So, here is the method:

template <class DATA>
void GIDTable::ReadFromStream(LStream *inStream)
{
   DATA data;

   ...

   data = new DATA;
   (*data).ReadFromStream(inStream);

   ...
}


The problem occurs on the second line.  data is of type DATA, so you
really want to allocate a new (*DATA).  IE, if DATA is a CBar *, then data
is a CBar *,
so we want it to say: data = new CBar;

How can I do this, only knowing the pointer type?
IE, is there a version of new that can take the pointer type, rather than
the actual type?  RTTI is fair game.


Please Help, this is an actual problem!

      -Adam

--
Adam Nash
CS 108 TA
adamnash@cs.stanford.edu
http://www-leland.stanford.edu/~smashman
Stanford University - Computer Science
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]