Why a static const variable assigned from offsetof gets an error if it is not marked inline?

62 views Asked by At

For some reason, this code runs (!) but with the raised error if I do not add inline. And works fine, otherwise. Why so?

#include <iostream>

#define NAME b

template <typename T>
struct B {
  static const ptrdiff_t shift = offsetof(T, NAME);  // it runs (!) but gives an error
  // inline static const ptrdiff_t shift = offsetof(T, NAME);  // works fine
};

struct A {
  B<A> b;
};

int main() {
  return 0; 
}

offsetof is underlined with a red squiggly line:

Offsetof of incomplete type 'A'clang(offsetof_incomplete_type)

0

There are 0 answers