代码之家  ›  专栏  ›  技术社区  ›  Tom de Geus

重载特征::矩阵基<T>

  •  1
  • Tom de Geus  · 技术社区  · 7 年前

    Eigen::MatrixBase<T> . 问题在于重载,编译器无法识别最接近的匹配项

    #include <iostream>
    #include <Eigen/Eigen>
    
    template <class T>
    void foo(const Eigen::MatrixBase<T> &data)
    {
      std::cout << "Eigen" << std::endl;
    }
    
    // ... several other overloads
    
    template <class T>
    void foo(const T &data)
    {
      std::cout << "other" << std::endl;
    }
    
    int main()
    {
      Eigen::VectorXd a(2);
    
      a(0) = 0.;
      a(1) = 1.;
    
      foo(a);
    }
    

    因此,输出是 other . 如何使本征超载,使其与任何本征矩阵最接近?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Piotr Skotnicki    7 年前

    Eigen::VectorXd 是一个用于 Eigen::Matrix<double, Dynamic, 1> . 向前走, Eigen::MatrixBase<T> Eigen::Matrix<T> . 在重载解析中,引用的实例的绑定 特征向量 const Eigen::VectorXd& 参数具有通过派生到基转换获胜的精确匹配排名(由 void foo(const Eigen::MatrixBase<T>&

    #include <type_traits>
    #include <utility>
    
    namespace detail
    {
        template <typename T>
        std::true_type test(const volatile Eigen::MatrixBase<T>&);
        std::false_type test(...);
    }
    
    template <typename T>
    using is_eigen_matrix = decltype(detail::test(std::declval<T&>()));
    
    template <class T>
    void foo(const Eigen::MatrixBase<T>& data)
    {
    }
    
    template <class T>
    auto foo(const T& data)
        -> typename std::enable_if<not is_eigen_matrix<T>::value>::type
    {
    }
    

    DEMO