<h3 dir="auto">System Information</h3>
<p dir="auto">OpenCV version: 4.5.0<br>
Operating System / Platform: Windows 10<br>
Compiler &amp; compiler version: GCC 9.3.0</p>
<h3 dir="auto">Detailed description</h3>
<p dir="auto">When trying to save a matrix that has the depth CV_16F using cv::FileStorage the assertion <code class="notranslate">depth &gt;=0 &amp;&amp; depth &lt;= CV_64F in function 'cv::fs::typeSymbol'</code> fails.</p>
<hr>
<p dir="auto">A possible explanation of the error might be found in <code class="notranslate">opencv2/core/hal/interface.h</code>. The order of the different depth definitions is incorrect and therefore the assertion fails:</p>
<div class="highlight highlight-source-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#define CV_8U   0
#define CV_8S   1
#define CV_16U  2
#define CV_16S  3
#define CV_32S  4
#define CV_32F  5
#define CV_64F  6
#define CV_16F  7"><pre class="notranslate">#<span class="pl-k">define</span> <span class="pl-en">CV_8U</span>   <span class="pl-c1">0</span>
#<span class="pl-k">define</span> <span class="pl-en">CV_8S</span>   <span class="pl-c1">1</span>
#<span class="pl-k">define</span> <span class="pl-en">CV_16U</span>  <span class="pl-c1">2</span>
#<span class="pl-k">define</span> <span class="pl-en">CV_16S</span>  <span class="pl-c1">3</span>
#<span class="pl-k">define</span> <span class="pl-en">CV_32S</span>  <span class="pl-c1">4</span>
#<span class="pl-k">define</span> <span class="pl-en">CV_32F</span>  <span class="pl-c1">5</span>
#<span class="pl-k">define</span> <span class="pl-en">CV_64F</span>  <span class="pl-c1">6</span>
#<span class="pl-k">define</span> <span class="pl-en">CV_16F</span>  <span class="pl-c1">7</span></pre></div>
<p dir="auto">Therefore <code class="notranslate">CV_64F &lt;= CV_16F</code> and if <code class="notranslate">CV_16F = depth</code> then <code class="notranslate">depth &lt;= CV_64F</code> is <code class="notranslate">false</code> and the assertion failed. I did not manage to update opencv to 4.7.0 using vcpkg and therefore I could not verify if the issue still persists beyond 4.5.0.</p>
<h3 dir="auto">Steps to reproduce</h3>
<div class="highlight highlight-source-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="cv::Mat test = cv::Mat::zeros({100, 100}, CV_16F);
cv::FileStorage file(&quot;test.yml&quot;, cv::FileStorage::WRITE);
file.write(&quot;Test Mat&quot;, test);"><pre class="notranslate">cv::Mat test = cv::Mat::zeros({<span class="pl-c1">100</span>, <span class="pl-c1">100</span>}, CV_16F);
cv::FileStorage <span class="pl-en">file</span>(<span class="pl-s"><span class="pl-pds">"</span>test.yml<span class="pl-pds">"</span></span>, cv::FileStorage::WRITE);
file.write(<span class="pl-s"><span class="pl-pds">"</span>Test Mat<span class="pl-pds">"</span></span>, test);</pre></div>
<h3 dir="auto">Issue submission checklist</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I report the issue, it's not a question</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I updated to the latest OpenCV version and the issue is still there</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> There is reproducer code and related data files (videos, images, onnx, etc)</li>
</ul>
<p dir="auto">I'm seeking an approach for <code class="notranslate">CV_NODISCARD</code> that will work across all the compilers opencv 4.x and above supports.</p>
<p dir="auto">The current approach is legacy for gcc and clang. Using <code class="notranslate">__attribute__((__warn_unused_result__))</code> on functions. No classes. And always at the end of the function declaration. This is supported on gcc 4.8.1 and higher (since 4.8.1 is needed for C++11).</p>
<p dir="auto">MSVC supports the standard attribute approach of <code class="notranslate">[[nodiscard]]</code>. It can be on classes, functions, vars. And it goes before the name of the item. At the end is not allowed. The support is available with VS2017 v15.3 and later; with /std:c++17 or higher.</p>
<p dir="auto">So these are somewhat mutually exclusive. One or the other compiler (gcc vs msvc) will complain with warnings. It should still compile. Both compilers have a method to disable the warnings, but I read on the internet that on some versions of gcc, it ignores the ignore command and still puts the warning.</p>
<p dir="auto">Does the core team have a recommendation?</p>