Search code examples
c++visual-c++openmp

MSVC Error C3015 (bad OpenMP for loop) but not an obvious mistake


I'm compiling some C++ code in Visual Studio 2022. I'm using /std:c++17 /O2 /permissive- /openmp:llvm /MP /arch:AVX2 as compiler settings. I'm getting the compiler error

C3015   initialization in OpenMP 'for' statement has improper form

However, I'm feeling like this might be a compiler bug (possibly with /openmp:llvm), since I can't even make the simplest OpenMP for loop work in this context. The for loop lives inside a lambda that's defined inside a template function that lives in a namespace (outside any class or struct) in a header file. Something like:

namespace Foo {
template <typename Func>
void f(Func work, const int beta, bool check = false) {
  auto mylambda = [](std::vector<double>& y, double a) {
      #pragma omp parallel for
      for (int test = 0; test < 0; test++)
          printf("Hello C3015\n");
  };

  work();
}
}

I have seen C3015 in the past when trying to use long (signed/unsigned) loop variables, but this is not that. Also, the exact code compiles fine under gcc, so this is something specific about MSVC behavior. There are other OpenMP for loops in this header file (in other functions etc.) that do not throw this error.

I'm just wondering whether there is some usage mistake I'm making, or if there is some workaround. Or if this is an unavoidable compiler bug, at least how to report such a thing and maybe get it fixed.


Solution

  • As suggested in the comment by @Jerry Coffin, this is a compiler bug that is a regression between x64 msvc 19.31 and 19.32. It turns out this bug has already been reported to Microsoft and a fix is pending release (I would guess in the next minor MSVC update).