postgres.git / summary / log / commit / refs

commit    baf7c93ed583949d837bc13f24a15a0efbcb1ae7
Author:   Thomas Munro <tmunro@postgresql.org>
Date:     Mon Dec 11 20:31:44 2023 +0000
First released in: 17.0

    Define unconstify() and unvolatize() for C++.
    
    These two macros wouldn't work if used in an inline function definition
    in a header seen by g++, because __builtin_types_compatible_p is only
    available in C.  Redirect to standard C++ const_cast (which also
    adds/removes volatile despite its name).
    
    Per cpluspluscheck failure in a development branch.
    
    Suggested-by: Peter Eisentraut <peter@eisentraut.org>
    Discussion: https://postgr.es/m/CA%2BhUKGK3OXFjkOyZiw-DgL2bUqk9by1uGuCnViJX786W%2BfyDSw%40mail.gmail.com


src/include/c.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/include/c.h b/src/include/c.h index 82f8e9d4c7b..4b0f5138d83 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -1245,7 +1245,10 @@ typedef union PGAlignedXLogBlock * Note that this only works in function scope, not for global variables (it'd * be nice, but not trivial, to improve that). */ -#if defined(HAVE__BUILTIN_TYPES_COMPATIBLE_P) +#if defined(__cplusplus) +#define unconstify(underlying_type, expr) const_cast<underlying_type>(expr) +#define unvolatize(underlying_type, expr) const_cast<underlying_type>(expr) +#elif defined(HAVE__BUILTIN_TYPES_COMPATIBLE_P) #define unconstify(underlying_type, expr) \ (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), const underlying_type), \ "wrong cast"), \ [parent: 0a5c46a7a488]