Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to resolve automatic return type C++ - continuation
    primarykey
    data
    text
    <p>The following code is compiling on gcc only when I comment the method dotProduct(It source, const size_t size), but I need to fix it somehow so that it compiles with this method uncommented. The names of methods must stay same as my intention is automatic detection of return types. Can someone please help me how to mend the code to make it compile?</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;iterator&gt; #include &lt;iostream&gt; #include &lt;limits&gt; using std::cerr; //! Template IF predicate implementation template &lt;bool B, typename TrueResult, typename FalseResult&gt; class TemplateIf { public: //! The result of template IF predicate typedef TrueResult Result; }; //! Template IF predicate implementation - specialization for false condition template &lt;typename TrueResult, typename FalseResult&gt; class TemplateIf&lt;false, TrueResult, FalseResult&gt; { public: //! The result of template IF predicate typedef FalseResult Result; }; template &lt;typename T1, typename T2&gt; class DetermineComputationType { public: //! The determined result type // If (isSpecialized(T1) &amp;&amp; isSpecialized(T2)) { typedef typename TemplateIf&lt; std::numeric_limits&lt;T1&gt;::is_specialized &amp;&amp; std::numeric_limits&lt;T2&gt;::is_specialized, // If (! isInteger(T1) &amp;&amp; isInteger(T2) ) // return T1; typename TemplateIf&lt; ! std::numeric_limits&lt;T1&gt;::is_integer &amp;&amp; std::numeric_limits&lt;T2&gt;::is_integer, T1, // Else if (! isInteger(T2) &amp;&amp; isInteger(T1) ) // return T2; typename TemplateIf&lt; ! std::numeric_limits&lt;T2&gt;::is_integer &amp;&amp; std::numeric_limits&lt;T1&gt;::is_integer, T2, // Else if ( sizeof(T1) &gt; sizeof(T2) ) // return T1; typename TemplateIf&lt; (sizeof(T1) &gt; sizeof(T2)), T1, // Else if ( sizeof(T2) &gt; sizeof(T1) ) // return T2; typename TemplateIf&lt; (sizeof(T2) &gt; sizeof(T1)), T2, // Else if ( isSigned(T2) ) // return T1; // Else // return T2; // } typename TemplateIf&lt; std::numeric_limits&lt;T2&gt;::is_signed, T1, T2&gt;::Result &gt;::Result &gt;::Result &gt;::Result &gt;::Result, // Else if ( sizeof(T2&gt; &gt; sizeof(T1) ) // return T2; // Else // return T1; typename TemplateIf&lt; (sizeof(T2) &gt; sizeof(T1)), T2, T1 &gt;::Result &gt;::Result Result; }; template &lt;typename It1,typename It2&gt; struct DotProduct { typedef typename std::iterator_traits&lt;It1&gt;::value_type VT1; typedef typename std::iterator_traits&lt;It2&gt;::value_type VT2; typedef typename DetermineComputationType&lt;VT1,VT2&gt;::Result Result; }; template &lt;typename R, typename Ct, typename It&gt; inline R dotProductRCtIt(It source, const size_t size) { Ct result = Ct(); for (size_t i = 0; i &lt; size; ++i) result += static_cast&lt;Ct&gt;(source[i]) * static_cast&lt;Ct&gt;(source[i]); return static_cast&lt;R&gt;(result); } //! For description see cpputil::dotProduct() template &lt;typename R, typename Ct, typename It, typename It2&gt; inline R dotProductRCtItIt2(It source, It2 source2, const size_t size) { Ct result = Ct(); for (size_t i = 0; i &lt; size; ++i) result += static_cast&lt;Ct&gt;(source[i]) * static_cast&lt;Ct&gt;(source2[i]); return static_cast&lt;R&gt;(result); } template &lt;typename T&gt; struct DetermineSingleType { typedef typename std::iterator_traits&lt;T&gt;::value_type Result; }; //! Convenience method - see above for description // !!! COMMENT THIS METHOD AND IT WILL START WORKING !!! template &lt;typename It&gt; inline typename DetermineSingleType&lt;It&gt;::Result dotProduct(It source, const size_t size) { typedef typename DetermineSingleType&lt;It&gt;::Result ItType; return dotProductRCtIt&lt;ItType, ItType, It&gt;(source, size); } template&lt;typename Result,typename It, typename It2&gt; Result dotProduct(It source1, It2 source2, const size_t size) { //typedef typename std::iterator_traits&lt;It&gt;::value_type ItType; //typedef typename std::iterator_traits&lt;It2&gt;::value_type It2Type; //typedef typename DetermineComputationType&lt;Result, ItType&gt;::Result Ct; typedef typename DotProduct&lt;It, It2&gt;::Result Ct; return dotProductRCtItIt2&lt;Result, Ct, It, It2&gt;(source1, source2, size); } template&lt;typename It1, typename It2&gt; typename DotProduct&lt;It1,It2&gt;::Result dotProduct(It1 source1, It2 source2, const size_t size) { typedef typename DotProduct&lt;It1,It2&gt;::Result Result; return dotProductRCtItIt2&lt;Result, Result, It1, It2&gt;(source1, source2, size); } template&lt;typename R, typename Ct, typename It, typename It2&gt; R dotProduct(It source, It2 source2, const size_t size) { return dotProductRCtItIt2&lt;R, Ct, It, It2&gt;(source, source2, size); } int main(int argc,char **argv) { const char *s1 = "abc"; const char *s2 = "def"; cerr &lt;&lt; dotProduct&lt;int&gt;(s1,s2,3) &lt;&lt; "\n"; cerr &lt;&lt; dotProduct(s1,s2,3) &lt;&lt; "\n"; return EXIT_SUCCESS; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload