1

The following program is failing when compiling with Clang with error: constexpr variable 'struct2Var' must be initialized by a constant expression {var, 2100433} .

If I remove __attribute__((weak)) from "var" declaration, it is passing without any issues.

Can somebody please explain the theory/reason behind this error.

struct myStruct
{
public:
 constexpr operator const wchar_t*() const
 {
  return &m_cch;
 }

 const wchar_t m_cch;
};

extern  __attribute__((weak)) const constexpr myStruct var {'a'};

struct myStruct2
{
 const wchar_t* stzKey = nullptr;

 int intvar = 0;
};

static constexpr const myStruct2 struct2Var[1]
{
  {var, 2100433}
};
Pendyala
  • 585
  • 1
  • 6
  • 17

1 Answers1

3

It looks like using __attribute__((weak)) discards the constexpr qualifier with clang but not with gcc. Despite clang trying to be a drop in replacement for gcc, it might implement such non standard feature differently. In that case, I would say that neither gcc nor clang is wrong.

Also, global constexpr might be tricky to maintain as they should all be defined consistently in every translation unit. To face this issue, inline variable have been added to c++17.

Meatboy 106
  • 196
  • 7