Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 21994

GCC constexpr allows add but not bitwise-or with address

$
0
0

Consider this code:

#include <cstdint>

static int x = 0;

const uintptr_t arithmetic()
{
    static constexpr uintptr_t result = ((uintptr_t)&x) + 1u;
    return result;
}

const uintptr_t bitwise()
{
    static constexpr uintptr_t result = ((uintptr_t)&x) | 1u;
    return result;
}

GCC (all versions 4-9) compiles arithmetic() just fine, but rejects bitwise():

<source>: In function 'const uintptr_t bitwise()':
<source>:13:57: error: '(((uintptr_t)(& x)) | 1)' is not a constant expression
   13 |     static constexpr uintptr_t result = ((uintptr_t)&x) | 1u;
      |                                         ~~~~~~~~~~~~~~~~^~~~

Why? Note that bitwise-or works fine in other constexpr use cases, but not this one.

Demo: https://godbolt.org/z/x5jbuU


Viewing all articles
Browse latest Browse all 21994

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>