Within a c++ consteval method, is it allowed to refer to the name of a static inline variable to enforce instantiation?

53 Views Asked by At

Is this valid c++ 20 or does the code rely on an undefined behavior?

#include <iostream>

template<int id>
struct Registration
{
    consteval auto operator() () const noexcept
    {
        // the only side effect of this call is the instantiation of sScope
        sScope.forceInstantiation();

        return id;
    }

private:
  struct Scope
  {
      Scope ()
      {
        std::cout << "Registered value " << id << " before main\n";
      }

      consteval static void forceInstantiation () noexcept
      {
      }
  };

  static const inline Scope sScope = {};
};

int main ()
{
    std::cout << "main begins\n";
    constexpr auto a = Registration<0> {} ();
}

This code compiles fine with all three major compilers (GCC, CLANG, MSVC), s.a. https://godbolt.org/z/drszovs5K.

0

There are 0 best solutions below