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

why ((size_t) &((TYPE *)0)->MEMBER) is not considered as constexpr since g++7.4

$
0
0

following code can be compiled successfully in g++7.3

#include <iostream>struct IMsg{    int i;    int j;};int func(){    constexpr size_t offset = ((size_t)&(((IMsg*)0)->j));    std::cout << offset << std::endl;}

but since g++7.4, I always got the compilation error as below

main.cpp:44:75: error: dereferencing a null pointer in '*0' constexpr size_t offset = ((size_t)&(((IMsg*)0)->j));                                                    ^main.cpp:44:75: error: conversion from pointer type ‘uint16_t* {aka short unsigned int*}’ to arithmetic > type ‘size_t {aka long unsigned int}’ in a constant expression

if I remove constexpr before size_t, it can be compiled successfully.

I don't get why constexpr can't be used here as the result can sure be known at compile time

and this is also the implementation of offsetof if builtin_offsetof is not provided.


Viewing all articles
Browse latest Browse all 22171


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