Generic functions to convert to void* and from void*

TSalm TSalm at free.fr
Sun Feb 22 08:11:30 PST 2009


> I'm trying to build function which have the hability to convert a type  
> to void* and from void*.
> I must use "ref" in the "toPtr" function because of this :
> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D.learn&artnum=15600
>
> Do you think that what I done is correct ?
>
>
> /* --------- CODE --------- */
>
> /***
>   * Convert a value to a void*
>   * Params:
>   *     val =
>   * Returns:
>   */
> void* toPtr(T)(ref T val)
> {
>    void* p ;
>
>    static if ( is( T b : Type ) )
>    {
>      p = new T ;
>      p = &val ;
>    }
>    else
>    {
>      p = &val ;
>    }
>
>    return p ;
> }
>
>
> /***
>   * Convert a void* to his value
>   * Params:
>   *     ptr =
>   * Returns:
>   */
> T fromPtr(T)(void* ptr)
> {
>    return *(cast(T*)ptr) ;
> }
> /* ------- END CODE -------- */
>

I've forget to say that toPtr can't be call with constants.


More information about the Digitalmars-d-learn mailing list