簡體   English   中英

如何解決自動返回類型C ++-繼續

[英]How to resolve automatic return type C++ - continuation

以下代碼僅在我注釋方法dotProduct(It source,const size_t size)時才在gcc上編譯,但我需要以某種方式對其進行修復,以使其在不注釋該方法的情況下進行編譯。 方法的名稱必須保持不變,因為我的意圖是自動檢測返回類型。 有人可以幫我如何修改代碼使其編譯嗎?

#include <stdlib.h> 
#include <iterator> 
#include <iostream>  
#include <limits>

using std::cerr; 

   //! Template IF predicate implementation
  template <bool B, typename TrueResult, typename FalseResult> 
  class TemplateIf {
  public:
    //! The result of template IF predicate
    typedef TrueResult Result;
  };

  //! Template IF predicate implementation - specialization for false condition
  template <typename TrueResult, typename FalseResult>
  class TemplateIf<false, TrueResult, FalseResult> {
  public:
    //! The result of template IF predicate
    typedef FalseResult Result;
  };


  template <typename T1, typename T2>
  class DetermineComputationType {
  public:
    //! The determined result type
    // If (isSpecialized(T1) && isSpecialized(T2)) {
    typedef typename TemplateIf< std::numeric_limits<T1>::is_specialized && std::numeric_limits<T2>::is_specialized,
      // If (! isInteger(T1) && isInteger(T2) )
      //   return T1;
      typename TemplateIf< ! std::numeric_limits<T1>::is_integer && std::numeric_limits<T2>::is_integer, T1,
      // Else if (! isInteger(T2) && isInteger(T1) )
      //   return T2;
      typename TemplateIf< ! std::numeric_limits<T2>::is_integer && std::numeric_limits<T1>::is_integer, T2,
      // Else if ( sizeof(T1) > sizeof(T2) )
      //   return T1;
      typename TemplateIf< (sizeof(T1) > sizeof(T2)), T1,
      // Else if ( sizeof(T2) > sizeof(T1) )
      //   return T2;
      typename TemplateIf< (sizeof(T2) > sizeof(T1)), T2,
      // Else if ( isSigned(T2) )
      //   return T1;
      // Else
      //   return T2; 
      // }
      typename TemplateIf< std::numeric_limits<T2>::is_signed, T1, T2>::Result >::Result >::Result >::Result >::Result,
      // Else if ( sizeof(T2> > sizeof(T1) )
      //   return T2;
      // Else
      //   return T1;
      typename TemplateIf< (sizeof(T2) > sizeof(T1)), T2, T1 >::Result >::Result Result;
  };


template <typename It1,typename It2> struct DotProduct 
{   
  typedef typename std::iterator_traits<It1>::value_type VT1;   
  typedef typename std::iterator_traits<It2>::value_type VT2;   
  typedef typename DetermineComputationType<VT1,VT2>::Result Result;
};  

  template <typename R, typename Ct, typename It>
  inline R dotProductRCtIt(It source, const size_t size)
  {
    Ct result = Ct();
    for (size_t i = 0; i < size; ++i)
      result += static_cast<Ct>(source[i]) * static_cast<Ct>(source[i]);

    return static_cast<R>(result);
  }

 //! For description see cpputil::dotProduct()
  template <typename R, typename Ct, typename It, typename It2>
  inline R dotProductRCtItIt2(It source, It2 source2, const size_t size) {
    Ct result = Ct();
    for (size_t i = 0; i < size; ++i)
      result += static_cast<Ct>(source[i]) * static_cast<Ct>(source2[i]);

    return static_cast<R>(result);
  }

template <typename T>
struct DetermineSingleType {
  typedef typename std::iterator_traits<T>::value_type Result;
};

//! Convenience method - see above for description
// !!! COMMENT THIS METHOD AND IT WILL START WORKING !!!
template <typename It> 
inline typename DetermineSingleType<It>::Result  dotProduct(It source, const size_t size) {
  typedef typename DetermineSingleType<It>::Result ItType;

  return dotProductRCtIt<ItType, ItType, It>(source, size);
}

template<typename Result,typename It, typename It2> Result dotProduct(It source1, It2 source2, const size_t size) 
{
  //typedef typename std::iterator_traits<It>::value_type ItType;
  //typedef typename std::iterator_traits<It2>::value_type It2Type;
  //typedef typename DetermineComputationType<Result, ItType>::Result Ct;
 typedef typename DotProduct<It, It2>::Result Ct;

  return dotProductRCtItIt2<Result, Ct, It, It2>(source1, source2, size);
}  

template<typename It1, typename It2> typename DotProduct<It1,It2>::Result   dotProduct(It1 source1, It2 source2, const size_t size) 
{   
  typedef typename DotProduct<It1,It2>::Result Result;   
  return dotProductRCtItIt2<Result, Result, It1, It2>(source1, source2, size);
}

template<typename R, typename Ct, typename It, typename It2> R dotProduct(It source, It2 source2, const size_t size)
{
 return dotProductRCtItIt2<R, Ct, It, It2>(source, source2, size);
}

int main(int argc,char **argv) 
{   
  const char *s1 = "abc";   
  const char *s2 = "def";   
  cerr << dotProduct<int>(s1,s2,3) << "\n";   
  cerr << dotProduct(s1,s2,3) << "\n";   
  return EXIT_SUCCESS; 
}

您正在將int類型傳遞給iterator_traits。 iterator_traits模板僅適用於公開適當的typedef的指針和類。

現在,對於我來說,為什么編譯器在經歷第一個重載時會引發錯誤尚不清楚,尤其是對於您正在進行的調用而言,這種重載是不正確的。 我會說這看起來像是編譯器中的錯誤,但我不是100%確定。

問題在於結果類型確定類需要依賴於函數參數。 一種解決方案似乎是為此“濫用” const size_t size參數:

模板類型名稱確定類型::值類型dotProduct(它的來源,聖大小){...}

調用:dotProduct((char *)s1,3);

這樣,直到確定St的類型后,才能運行結果類型確定,因此參數匹配過程開始將使用的參數與模板類型進行匹配,並且失敗,因為無法從char *到int進行隱式轉換。

但是,此解決方案的壞處是必須將const size_t size參數進行模板化和濫用。 添加具有默認值的另一個虛擬參數不會防止編譯錯誤。

目前,我不知道有任何其他更好的解決方案,並且濫用size參數的解決方案也不是很好。 因此,很可能我將不得不放棄自動返回類型的確定。 還有其他想法嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM