Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to resolve automatic return type - C++ (templates)
    primarykey
    data
    text
    <p>I have a dotProduct methods with these signatures:</p> <pre><code>template&lt;typename It, typename It2&gt; typename DetermineResultType&lt; typename std::iterator_traits&lt;It&gt;::value_type, typename std::iterator_traits&lt;It2&gt;::value_type&gt;::Result dotProduct(It source, It2 source, const size_t size); template&lt;typename R, typename It, typename It2&gt; R dotProduct(It source, It2 source, const size_t size); </code></pre> <p>Everything works ok, until I try:</p> <pre><code>char * s, * s2; int result = dotProduct&lt;int&gt;(s, s2, 3); </code></pre> <p>With this I get error that <code>std::iterator_traits&lt;int&gt;::value_type</code> is not defined.</p> <p>How can I resolve this so that I can automatically determine result type for the first declaration? (The name of methods must stay same). I employ some rules to determine the resulting type, but this is not important.</p> <p>Basically the problem boils down to whether <code>std::iterator_traits&lt;It&gt;::value_type</code> is defined - when non-specialized std::iterator_traits is used. Can I detect this somehow? If not what other options do I have?</p> <p>Ok, now the example down works, but how do I do the same trick my DetermineComputationType aka DetermineResultType is defined this way: ???</p> <pre><code> //! 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; }; /*! \brief Helper class for computation type determination - works for 3 types \tparam T1 The first type for computation type determination \tparam T2 The second type for computation type determination \tparam T3 The third type for computation type determination The result is computed in order: (T1, (T2, T3)) */ template &lt;typename T1, typename T2, typename T3&gt; class DetermineComputationType2 { public: //! The computation type result typedef typename DetermineComputationType&lt;T1, typename DetermineComputationType&lt;T2, T3&gt;::Result &gt;::Result Result; }; template &lt;typename T1, typename T2&gt; class DetermineReturnComputationType { public: typedef typename std::iterator_traits&lt;T1&gt;::value_type T1Type; typedef typename std::iterator_traits&lt;T2&gt;::value_type T2Type; typedef typename DetermineComputationType&lt;T1Type, T2Type&gt;::Result 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); } /*! \brief Returns the dot product of vectors \param[in] source The iterator to the start of source vector \param[in] source2 The iterator to the start of second source vector \param[in] size The size of vector(s) */ template &lt;typename R, typename Ct, typename It, typename It2&gt; inline R dotProduct(It source, It2 source2, const size_t size) { return arithmeticsInternal::dotProductRCtItIt2&lt;R, Ct, It, It2&gt;(source, source2, size); } //! Convenience method - see above for description template &lt;typename R, typename It, typename It2&gt; inline R dotProduct(It source, 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 arithmeticsInternal::DetermineComputationType2&lt;R, ItType, It2Type&gt;::Result Ct; return arithmeticsInternal::dotProductRCtItIt2&lt;R, Ct, It, It2&gt;(source, source2, size); } //! Convenience method - see above for description template &lt;typename It, typename It2&gt; inline typename arithmeticsInternal::DetermineReturnComputationType&lt;It, It2&gt;::Result dotProduct(It source, 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 arithmeticsInternal::DetermineReturnComputationType&lt;It, It2&gt;::Result R; return arithmeticsInternal::dotProductRCtItIt2&lt;R, R, It, It2&gt;(source, source2, size); } </code></pre>
    singulars
    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