microsoft / GSL

Guidelines Support Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gsl::not_null and std::variant are not fully compatible

mymedia2 opened this issue · comments

I suppose it is worth highlighting a lack of interoperability between gsl::not_null and std::variant from libstdc++. Consider this code.

#include <variant>
#include <gsl/pointers>

int main()
{
  int i = 0;
  std::variant< std::monostate, gsl::not_null<int*> > v;
  v.emplace< gsl::not_null<int*> >(&i);
}

https://godbolt.org/z/KTP9EdMzf → run the code above in the playground

It segfaults inside std::variant::emplace because of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106547. However, a workaround of that libstdc++'s bug may be added in GSL. Say if the gsl::not_null<int*> class had a noexcept move constructor, an optimized path without the bug could be chosen.