How to allocate an element of type T with value x in generic code?

John Colvin john.loughran.colvin at gmail.com
Wed Apr 3 07:47:21 PDT 2013


On Wednesday, 3 April 2013 at 11:05:06 UTC, Tobias Pankrath 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?

Do you need to use new? i.e. do you need the variable to be 
allocated on the heap?
Also, as you've written it, px is not a pointer to x which is a 
bit misleading. What is the result you actually want?


More information about the Digitalmars-d-learn mailing list