https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127353

            Bug ID: 127353
           Summary: undefined reference to static variable found via
                    reflection
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ivan.lazaric.gcc at gmail dot com
  Target Milestone: ---

Flags: "-std=c++26 -freflection"

```cpp
#include <meta>

template<typename>
struct storage {
  static inline int value = 2;
};

consteval int& find_value(std::meta::info type) {
  for (auto member : members_of(type, std::meta::access_context::unchecked()))
{
    if (!has_identifier(member)) continue;
    if (identifier_of(member) != "value") continue;
    return extract<int&>(member);
  }
  throw;
}

int main() {
  // equivalent to: return storage<int>::value;
  return find_value(^^storage<int>);
}
```

Outcome:
```
/usr/bin/ld: /tmp/ccQokmqh.o: in function `main':
bug.cpp:(.text.startup+0x2): undefined reference to `storage<int>::value'
collect2: error: ld returned 1 exit status
```

Godbolt: https://godbolt.org/z/M6qaGjYT5

Noting two ways to get around this:
[[gnu::used]]: https://godbolt.org/z/hj5bq6b9P
different `find_value()` impl: https://godbolt.org/z/zjffbG4eG

Reply via email to