Andrew Talbot wrote:
David Laight wrote:
On Mon, Aug 07, 2006 at 09:54:20PM +0100, Andrew Talbot wrote:
would like to submit a patch that, for example, changes strchrW() to:
extern inline WCHAR *strrchrW( const WCHAR *str, WCHAR ch ) { WCHAR *ret = NULL; do { if (*str == ch) ret = (WCHAR *)(size_t)str; } while (*str++); return ret; }
why not just have:
extern inline void *__deconst(const void *v) { return (char *)0 + ((const char *)v - (const char *)0)); }
Then the code above could be: extern inline WCHAR *strrchrW( const WCHAR *str, WCHAR ch ) { do { if (*str == ch) return __deconst(str); } while (*str++); return 0; }
David
That's interesting. IMHO, it's a bit more complex and obscure, but seems to ensure portability. I shall be interested to know what others think.
what I don't like is that in order to plug a hole (casting from const foo* to foo*), we create a bigger hole by allowing to cast from const foo* to bar* (and the compiler will not give any warning) if we want to go into this, then I'd rather have _deconst explicitly use the type: #define __deconst(v,X) ({const X _v = (v); ((X)(int)_v);}) and in the previous example use __deconst(str, char*);
one could think of using gcc's typeof operator to avoid passing the type to deconst, but this doesn't seem possible (you have to express foo* in terms of const foo* which isn't possible AFAICT)
A+