🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Adam Nash Fri, 15 Mar 1996 00:12
Problem with new in templates...
#3677
Author: Adam Nash
Date: Fri, 15 Mar 1996 00:12
49 lines
1933 bytes
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                             ]
Re: Problem with new in templates...
#3680
Author: Fergus Henderson
Date: Fri, 15 Mar 1996 02:02
53 lines
2309 bytes
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                             ]
Re: Problem with new in templates...
#3686
Author: Tony Cook
Date: Fri, 15 Mar 1996 02:13
37 lines
1963 bytes
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                             ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads