为 Eigen 类型特化一个函数

Specialize a function for Eigen types

我正在编写一个模板化的稀疏容器 class 并想检查传入数据是否为零。数据类型将是整数或固定大小的 Eigen 类型。

#include <Eigen/Core>

template<typename T>
struct SparseContainer
{
    void insert(const T& value)
    {
        if(isZero(value))
            return;
        // ...
    }
};

int main(int argc, char* argv[])
{
    SparseContainer<int> a;
    a.insert(1);

    SparseContainer<Eigen::Vector2i> b;
    b.insert(Eigen::Vector2i(1, 0));
}

我如何提供 isZero() 函数,以便它在默认情况下使用整数和 Eigen 类型,并且可以由 class 的用户针对他们自己的类型进行扩展。我不使用 boost,但 C++11(即 std::enable_if)没问题。

基于:

#include <Eigen/Core>
#include <iostream>
#include <type_traits>

namespace is_eigen_detail {
    // These functions are never defined.
    template <typename T>
    std::true_type test(const Eigen::EigenBase<T>*);
    std::false_type test(...);
}

template <typename T>
struct is_eigen_type : public decltype(is_eigen_detail::test(std::declval<T*>()))
{};

template<class T>
typename std::enable_if<is_eigen_type<T>::value, bool>::type isZero(const T& x) { return x.isZero(); }

template<class T>
typename std::enable_if<!is_eigen_type<T>::value, bool>::type isZero(const T& x) { return x == 0; }

int main()
{
    Eigen::Vector3d a;
    Eigen::Array3d b;

    a.setZero();
    b.setRandom();

    std::cout << "a: " << isZero(a) << "\nb: " << isZero(b) << "\n0.0: " << isZero(0.0) << std::endl;
}

N.B.: 当然你可以使用Eigen::MatrixBaseEigen::DenseBase而不是Eigen::EigenBase,如果你只想专攻矩阵或矩阵和数组。