Lazy.h produces many compiler errors in UE project

I’m compiling an UnrealEngine 5.3 project that includes pytorch headers (git cloned the trunk this month). My system is Win 11, MSVS 17.11.5, and file from “include/c10/util/Lazy.h” produces these errors:

Error C2988 unrecognizable template declaration/definition RASample c:\src\pytorch222\torch\include\c10\util\Lazy.h 32
Error C2059 syntax error: ‘!’ RASample c:\src\pytorch222\torch\include\c10\util\Lazy.h 32
Error C2334 unexpected token(s) preceding ‘{’; skipping apparent function body RASample c:\src\pytorch222\torch\include\c10\util\Lazy.h 32
Error C2059 syntax error: ‘)’ RASample c:\src\pytorch222\torch\include\c10\util\Lazy.h 32
Error C2334 unexpected token(s) preceding ‘{’; skipping apparent function body RASample c:\src\pytorch222\torch\include\c10\util\Lazy.h 32
Other errors are:
Error C3861 ‘value_’: identifier not found RASample c:\src\pytorch222\torch\include\c10\util\Lazy.h 26
Error C3861 ‘value_’: identifier not found RASample c:\src\pytorch222\torch\include\c10\util\Lazy.h 22
Error C2065 ‘value_’: undeclared identifier RASample c:\src\pytorch222\torch\include\c10\util\Lazy.h 22

The problem is in the method:
template
T& ensure(const Factory& factory) {
if (T* value = value_.load(std::memory_order_acquire)) {
return value;
}
T
value = new T(factory());
T* old = nullptr;
if (!value_.compare_exchange_strong(
old, value, std::memory_order_release, std::memory_order_acquire)) {
delete value;
value = old;
}
return *value;
}
I’ve configured UE build system to use cpp17 by using:
CppStandard = CppStandardVersion.Cpp17;
in the C# build config file.

Tried changing cpp version to 14 - can’t compile because of the missed “fold expression” feature of the cpp14.
Tried changing cpp version to 20 - the errors are the same as for the cpp version 17.

Does anyone can help resolving the errors that I have?

1 Like

After going through the code many times I found out that ensure is a UE reserved macro and it conflicts with this function name. Defining #undef ensure on top of the OptimisticLazy class declaration of Lazy.h will resolve this issue.