模板类和“无效使用不完整类型”错误

时间:2019-10-23 23:37:38

标签: c++ class c++11 templates template-specialization

我有一个模板Matrix类,并且我使用了std::vector<std::vector<T>>来存储数据。 我需要专门研究std::complex矩阵的一些方法,例如:

template <typename T>
bool Matrix<T>::is_hermitian() const
{
   if (!(*this).is_squared())
      return false;
   for (int r = 0; r < rows_; r++)
      for (int c = 0; c < columns_; c++)
         if (mat[r][c] != mat[c][r])
            return false;
   return true;
}

对于专用方法,我认为是这样的:

template <typename T>
bool Matrix<std::complex<T> >::is_hermitian() const
{
   if (!(*this).is_squared())
      return false;
   for (int r = 0; r < rows_; r++)
      for (int c = 0; c < columns_; c++)
         if (mat[r][c] != std::conj(mat[c][r]))
            return false;
   return true;
}

但是编译器向我返回错误

'invalid use of incomplete type' 

我在.cpp文件的末尾实例化了一堆我可以在主程序中使用的类:

template class Matrix<int>;
template class Matrix<double>;
template class Matrix<float>;
template class Matrix< std::complex<float> >;
template class Matrix< std::complex<int> >;

如何为所有std::complex<T>类型实现一种方法?

如果您知道如何用Matrix< std::complex<T> >代替最后两个实例,我将非常感激。

2 个答案:

答案 0 :(得分:3)

T类实例化中的std::complexMatrix<T>时,您可以应用SFINE(即"Substitution Failure Is Not An Error")技术和函数重载来选择正确的方法。

以下是该想法的演示:See example code online live

#include <type_traits>  // std::enable_if, std::false_type

// traits for checking, T is `std::complex`
template<typename> struct is_std_complex : std::false_type {};
template<typename T> struct is_std_complex<std::complex<T>> : std::true_type {};

// traits for `std::enable_if` helpers
template<typename Type, typename ReType = void>
using EnabledForComplex = typename std::enable_if<is_std_complex<Type>::value, ReType>::type;

template<typename Type, typename ReType = void>
using EnabledNotForComplex = typename std::enable_if<!is_std_complex<Type>::value, ReType>::type;

template<typename T>
class Matrix
{
   // ...members

public:    
   template<typename Type = T>
   auto is_hermitian() const -> EnabledNotForComplex<Type, bool>
   {
      // ... code for non-std::complex types
   }

   template<typename Type = T>
   auto is_hermitian() const->EnabledForComplex<Type, bool>
   {
      // ... code for std::complex types
   }
};

也就是说,如果您有权访问,则可以使用if constexpr,它只会实例化分支,这在编译时是正确的。 See example code online live

#include <type_traits> // std::false_type

// traits for checking, T is `std::complex`
template<typename> struct is_std_complex : std::false_type {};
template<typename T> struct is_std_complex<std::complex<T>> : std::true_type {};

template<typename T>
class Matrix
{
   // ...members

public:
   bool is_hermitian() const
   {
      if (!is_squared()) return false;
      if constexpr (is_std_complex<T>::value)
      {
         // ... code for std::complex types
      }
      else
      {
         // ... code for non-std::complex types
      }
      return true;
   }
};

答案 1 :(得分:2)

您不能部分专门化功能模板,但是可以对模板进行分类。一种技巧是使成员函数委托给帮助程序类模板。像这样:

template <typename T>
struct IsHermitianHelper;

template <typename T>
class Matrix {
  friend class IsHermitianHelper<T>;
public:
  bool is_hermitian() const;
};

template <typename T>
struct IsHermitianHelper {
  static bool is_hermitian(const Matrix<T>& m) {
    // General implementation here
  }
};

template <typename T>
struct IsHermitianHelper<std::complex<T>> {
  static bool is_hermitian(const Matrix<std::complex<T>>& m) {
    // Special case for std::complex here
  }
};

template <typename T>
bool Matrix<T>::is_hermitian() const {
  return IsHermitianHelper<T>::is_hermitian(*this);
}