|
Boost : |
Subject: Re: [boost] [AFIO] Formal review
From: Niall Douglas (s_sourceforge_at_[hidden])
Date: 2015-09-02 14:55:18
On 2 Sep 2015 at 5:31, John Bytheway wrote:
> > Please do correct me if I am wrong, but I had thought that this is
> > defined behaviour:
> >
> > int a=5;
> > void *b=(void *)(size_t) a;
> > int c=(int)(size_t) b;
> > assert(c==a);
> >
> > This is certainly a very common pattern in C.
>
> I believe this is implementation-defined. Based on N3797
> [expr.reinterpret.cast], paragraph 5:
>
> "A value of integral type or enumeration type can be explicitly
> converted to a pointer. A pointer converted to an integer of sufficient
> size (if any such exists on the implementation) and back to the same
> pointer type will have its original value; mappings between pointers and
> integers are otherwise implementation-defined."
>
> The behaviour that is defined (pointer to integer and back) is the
> reverse of the one you want.
That's the whole point of the intermediate size_t cast. int to size_t
is defined behaviour, size_t to void * is defined behaviour, void *
to size_t is defined behaviour, size_t to int is defined behaviour.
Older code, and still a lot of C code, directly casts between int and
void * and back again, even though strictly speaking that's a no no.
Niall
-- ned Productions Limited Consulting http://www.nedproductions.biz/ http://ie.linkedin.com/in/nialldouglas/
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk