<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Templace Ambiguity auflösen]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgenden Code:</p>
<pre><code>// Compares two Eigen::MatrixBase for equality up to tolerance
template &lt;typename DerivedA, typename DerivedB&gt;
constexpr bool equals (const Eigen::MatrixBase&lt;DerivedA&gt;&amp; A,
                       const Eigen::MatrixBase&lt;DerivedB&gt;&amp; B,
                       double tolerance = NUMERICAL_ZERO_DIFFERENCE)
{
  return A.isApprox(B, tolerance);
}

// Compares two scalar types for equality up to tolerance
template&lt;class A, class B&gt;
constexpr bool equals(const A a, const B b, const double tolerance = NUMERICAL_ZERO_DIFFERENCE)
{
  return boost::math::relative_difference(a, b) &lt;= tolerance;
}
</code></pre>
<p>Nun habe ich das Problem, dass der Compiler auch für Objekte, die von Eigen::MatrixBase abgeleitet sind, dass untere Template verwendet.</p>
<p>Eine Lösung ist:</p>
<pre><code>bool equals(const double a, const double b, const double tolerance = NUMERICAL_ZERO_DIFFERENCE)
{
  return boost::math::relative_difference(a, b) &lt;= tolerance;
}
</code></pre>
<p>damit geht mir aber die Spezialisierung weg, die ich schon gerne behalten würde.</p>
<p>Wie kann ich den Compiler dazu überreden, dass erste Template zu verwenden und nur, wenn von der spezialisierten Funktionssignatur nicht passt, das untere zu verwenden?</p>
<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/339649/templace-ambiguity-auflösen</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 23:19:59 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/339649.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 16 Sep 2016 14:06:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Templace Ambiguity auflösen on Fri, 16 Sep 2016 14:06:58 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgenden Code:</p>
<pre><code>// Compares two Eigen::MatrixBase for equality up to tolerance
template &lt;typename DerivedA, typename DerivedB&gt;
constexpr bool equals (const Eigen::MatrixBase&lt;DerivedA&gt;&amp; A,
                       const Eigen::MatrixBase&lt;DerivedB&gt;&amp; B,
                       double tolerance = NUMERICAL_ZERO_DIFFERENCE)
{
  return A.isApprox(B, tolerance);
}

// Compares two scalar types for equality up to tolerance
template&lt;class A, class B&gt;
constexpr bool equals(const A a, const B b, const double tolerance = NUMERICAL_ZERO_DIFFERENCE)
{
  return boost::math::relative_difference(a, b) &lt;= tolerance;
}
</code></pre>
<p>Nun habe ich das Problem, dass der Compiler auch für Objekte, die von Eigen::MatrixBase abgeleitet sind, dass untere Template verwendet.</p>
<p>Eine Lösung ist:</p>
<pre><code>bool equals(const double a, const double b, const double tolerance = NUMERICAL_ZERO_DIFFERENCE)
{
  return boost::math::relative_difference(a, b) &lt;= tolerance;
}
</code></pre>
<p>damit geht mir aber die Spezialisierung weg, die ich schon gerne behalten würde.</p>
<p>Wie kann ich den Compiler dazu überreden, dass erste Template zu verwenden und nur, wenn von der spezialisierten Funktionssignatur nicht passt, das untere zu verwenden?</p>
<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2508824</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2508824</guid><dc:creator><![CDATA[Horus107]]></dc:creator><pubDate>Fri, 16 Sep 2016 14:06:58 GMT</pubDate></item><item><title><![CDATA[Reply to Templace Ambiguity auflösen on Fri, 16 Sep 2016 15:12:14 GMT]]></title><description><![CDATA[<p>Hey,<br />
generell gäbe es ja <code>std::is_base_of</code> , womit sich die Sache ziemlich einfach gestaltet. Da hier die Basisklasse ein Klassentemplate ist, müssen wir eine eigene Version davon schreiben. Hier eine Möglichkeit:</p>
<pre><code>namespace detail
{
    template&lt;typename T&gt;
    class is_derived_from_matrix_base_helper
    {
        static char (&amp;test(...))[1];
        template&lt;typename U&gt;    
        static char (&amp;test(Eigen::MatrixBase&lt;U&gt;*))[2];

    public:
        using type = std::integral_constant&lt;bool, sizeof(test(std::declval&lt;T*&gt;())) - 1&gt;;
    };
}
template&lt;typename T&gt;
struct is_derived_from_matrix_base : detail::is_derived_from_matrix_base_helper&lt;T&gt;::type
{
};
template&lt;typename T&gt;
constexpr bool is_derived_from_matrix_base_v = is_derived_from_matrix_base&lt;T&gt;::value;
</code></pre>
<p>Nun lässt sich die allgemeine Spezialisierung von <code>equals</code> mit SFINAE wie folgt formulieren:</p>
<pre><code>template&lt;class A, class B&gt;
constexpr std::enable_if_t&lt;!(is_derived_from_matrix_base_v&lt;A&gt; &amp;&amp; is_derived_from_matrix_base_v&lt;B&gt;), bool&gt;
	equals(A const&amp; a, B const&amp; b, double tolerance = NUMERICAL_ZERO_DIFFERENCE)
{
  return boost::math::relative_difference(a, b) &lt;= tolerance;
}
</code></pre>
<p>So nebenbei: <em>top-level CV-qualifiers</em> bei Parametern haben keine Auswirkung auf die Signatur, kann man daher getrost weglassen.</p>
<p>Gruss</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2508830</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2508830</guid><dc:creator><![CDATA[Fytch]]></dc:creator><pubDate>Fri, 16 Sep 2016 15:12:14 GMT</pubDate></item><item><title><![CDATA[Reply to Templace Ambiguity auflösen on Fri, 16 Sep 2016 22:12:32 GMT]]></title><description><![CDATA[<p>Fytch schrieb:</p>
<blockquote>
<p>So nebenbei: <em>top-level CV-qualifiers</em> bei Parametern haben keine Auswirkung auf die Signatur, kann man daher getrost weglassen.</p>
</blockquote>
<p>Schon, aber in der Definition machen sie Sinn, da Parameter auch nur Variablen sind.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2508841</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2508841</guid><dc:creator><![CDATA[Biolunar]]></dc:creator><pubDate>Fri, 16 Sep 2016 22:12:32 GMT</pubDate></item></channel></rss>