How to allocate an element of type T with value x in generic code?
Steven Schveighoffer
schveiguy at yahoo.com
Wed Apr 3 09:42:54 PDT 2013
On Wed, 03 Apr 2013 07:05:05 -0400, Tobias Pankrath <tobias at pankrath.net>
wrote:
> basic idea.
> ---
> T x;
> T* px = new T(x);
> ---
> int x
> int* px = new int(x); // fails
> ---
>
> I need to do this for structs and basic types. What's the standard way
> to do this?
A crude but easy way to do this:
int *px = [x].ptr;
Only caveat is that the allocated block may be slightly bigger than
necessary. If you are concerned about footprint, you may want to do it
with GC.malloc calls directly.
-Steve
More information about the Digitalmars-d-learn
mailing list