Problems compiling with Libtorch v1.5.0 and VS toolset v140

username: xmatus
cooked: <p>Hello,</p>
<p>I have a problem building minimal C++ example provided in the official docs (<a href="https://pytorch.org/cppdocs/installing.html#minimal-example" rel="nofollow noopener">https://pytorch.org/cppdocs/installing.html#minimal-example</a>) with Libtorch version 1.5.0 using VS 2015 C++ build tools (v140). I’m using the pre-built pytorch binaries downloaded from the official site.</p>
<h3>Minimal example app</h3>
<p><em>example-app.cpp</em></p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout &lt;&lt; tensor &lt;&lt; std::endl;
}
</code></pre>
<p><em>CMakeLists.txt</em></p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
</code></pre>
<p><em>Commands to build the example</em></p>
<pre><code class="lang-auto">mkdir build
cmake -G "Visual Studio 16" -A x64 -T v140 -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..
cmake --build build --config Release
</code></pre>
<p>I got bunch of warnings and errors complaining about <code>constexpr</code>:<br>
<strong>Warnings:</strong></p>
<pre><code class="lang-auto">warning C4814: in C++14 'constexpr' will not imply 'const'; consider explicitly specifying 'const'
</code></pre>
<p><strong>Errors:</strong> (selecting first few errors)</p>
<pre><code class="lang-auto">libtorch\include\c10/util/ConstexprCrc.h(103): error C3250: 'i': declaration is not allowed in 'constexpr' function body
libtorch\include\c10/util/ConstexprCrc.h(103): error C3249: illegal statement or sub-expression for 'constexpr' function
libtorch\include\c10/util/Optional.h(277): error C2556: 'c10::ScalarType &amp;c10::optional&lt;c10::ScalarType&gt;::contained_val(void) const &amp;': overloaded function differs only by return type from 'const c10::ScalarType &amp;c10::optional&lt;c10::ScalarType&gt;::contained_val(void) const &amp;'
...
</code></pre>
<p>I was able to compile this minimal example using newer VS toolsets (e.g. v142). Unfortunately, I need to use v140.</p>
<p><strong>Using older Pytorch versions (v1.3.1) however did not result in any errors</strong>. Using the version v1.4.0 there is only one error: <code>error C2872: 'std': ambiguous symbol</code>, which I was able to solve prepending <code>std</code> with <code>::</code> as suggested in <a href="https://discuss.pytorch.org/t/compiler-report-std-ambiguous-symbol-error-under-visual-c-2017/39459/2" class="inline-onebox">Compiler report "'std': ambiguous symbol" error under Visual C++ 2017</a>.</p>
<p>Could this be a bug in the actual Libtorch version or has Libtorch abandoned support for toolset v140?</p>
<p><strong>My environment:</strong><br>
Windows 10<br>
Compiler: MSVC 19.0.24245.0<br>
CUDA: v9.2</p>
<p>I will appreciate any help.</p>
<p>Thanks,<br>
Matus</p>

username: peterjc123
cooked: <p>I guess you can try to remove the <code>constexpr</code>s as reported in the error log.</p>
<blockquote>
<p>Could this be a bug in the actual Libtorch version or has Libtorch abandoned support for toolset v140?</p>
</blockquote>
<p>We didn’t test the support for toolset v140. And there’s no plan for maintaining its support given the fact the binaries are compatible across v140 to v142.</p>

username: xmatus
cooked: <p>Hello, thanks for the reply.</p>
<p>I tried, but other errors showed up, namely in header files <code>Optional.h</code>, <code>ScalarType.h</code> and others.</p>
<pre><code class="lang-auto">libtorch\include\c10/util/Optional.h(277): error C2556: 'c10::ScalarType &amp;c10::optional&lt;c10::ScalarType&gt;::contained_val(void) const &amp;': overloaded function differs only by return type from 'const c10::ScalarType &amp;c10::optional&lt;c10::ScalarType&gt;::contained_val(void) const &amp;'
libtorch\include\c10/util/Optional.h(277): error C2373: 'c10::optional&lt;c10::ScalarType&gt;::contained_val': redefinition; different type modifiers
...
</code></pre>
<p>I think the changes, that caused my problems were introduced in (<a href="https://github.com/pytorch/pytorch/pull/30916" rel="nofollow noopener">https://github.com/pytorch/pytorch/pull/30916</a>, <a href="https://github.com/pytorch/pytorch/pull/30919" rel="nofollow noopener">https://github.com/pytorch/pytorch/pull/30919</a> and probably others).</p>
<p>Correct me if I’m wrong, but that means there is requirement for C++14 compiler in version 1.5.0, but toolset v140 does not support entire standard --&gt; <em>extended constexpr</em> (<a href="https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019" rel="nofollow noopener">https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019</a>). So for the version 1.5.0 and future versions I have to use the newer toolset than v140?</p>