Lazy.h produces many compiler errors in UE project

lavoos: <p>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:</p>
<div class="md-table">
<table>
<thead>
<tr>
<th>Error</th>
<th>C2988</th>
<th>unrecognizable template declaration/definition</th>
<th>RASample</th>
<th>c:\src\pytorch222\torch\include\c10\util\Lazy.h</th>
<th>32</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>Error</td>
<td>C2059</td>
<td>syntax error: ‘!’</td>
<td>RASample</td>
<td>c:\src\pytorch222\torch\include\c10\util\Lazy.h</td>
<td>32</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Error</td>
<td>C2334</td>
<td>unexpected token(s) preceding ‘{’; skipping apparent function body</td>
<td>RASample</td>
<td>c:\src\pytorch222\torch\include\c10\util\Lazy.h</td>
<td>32</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Error</td>
<td>C2059</td>
<td>syntax error: ‘)’</td>
<td>RASample</td>
<td>c:\src\pytorch222\torch\include\c10\util\Lazy.h</td>
<td>32</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Error</td>
<td>C2334</td>
<td>unexpected token(s) preceding ‘{’; skipping apparent function body</td>
<td>RASample</td>
<td>c:\src\pytorch222\torch\include\c10\util\Lazy.h</td>
<td>32</td>
<td></td>
<td></td>
</tr>
<tr>
<td>…</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Other errors are:</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Error</td>
<td>C3861</td>
<td>‘value_’: identifier not found</td>
<td>RASample</td>
<td>c:\src\pytorch222\torch\include\c10\util\Lazy.h</td>
<td>26</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Error</td>
<td>C3861</td>
<td>‘value_’: identifier not found</td>
<td>RASample</td>
<td>c:\src\pytorch222\torch\include\c10\util\Lazy.h</td>
<td>22</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Error</td>
<td>C2065</td>
<td>‘value_’: undeclared identifier</td>
<td>RASample</td>
<td>c:\src\pytorch222\torch\include\c10\util\Lazy.h</td>
<td>22</td>
<td></td>
<td></td>
</tr>
<tr>
<td>…</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div><p>The problem is in the method:<br>
template <br>
T&amp; ensure(const Factory&amp; factory) {<br>
if (T* value = value_.load(std::memory_order_acquire)) {<br>
return <em>value;<br>
}<br>
T</em> value = new T(factory());<br>
T* old = nullptr;<br>
if (!value_.compare_exchange_strong(<br>
old, value, std::memory_order_release, std::memory_order_acquire)) {<br>
delete value;<br>
value = old;<br>
}<br>
return *value;<br>
}<br>
I’ve configured UE build system to use cpp17 by using:<br>
CppStandard = CppStandardVersion.Cpp17;<br>
in the C# build config file.</p>
<p>Tried changing cpp version to 14 - can’t compile because of the missed “fold expression” feature of the cpp14.<br>
Tried changing cpp version to 20 - the errors are the same as for the cpp version 17.</p>
<p>Does anyone can help resolving the errors that I have?</p>

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