以 Eigen 类型作为参数的通用函数回调

generic function callback with Eigen types as parameters

我正在尝试将使用 Eigen 对象作为输入和输出的函数作为回调传递给另一个函数。我找不到正确处理 const 参数的方法。例如我有这个复制功能:

//a generic function copying a matrix into another
template<
  typename DerivedV1,
  typename DerivedV2
  >
void copy
(
  const Eigen::MatrixBase<DerivedV1> & v1,
  Eigen::MatrixBase<DerivedV2> const & v2 //Eigen const hack, this is not const
)
{
  Eigen::MatrixBase<DerivedV2> & v2_cast = 
    const_cast< Eigen::MatrixBase<DerivedV2> & >(v2) ;
  v2_cast = v1 ;
}

我知道 output parameters can be considered a bad practice but I'm trying to follow the coding guidelines of libigl。作为参考,我在另一个函数中调用这个函数,这会起作用。

//reference non generic behaviour
template<
  typename DerivedV1,
  typename DerivedV2
  >
void reference
(
  const Eigen::MatrixBase<DerivedV1> & V1,
  Eigen::MatrixBase<DerivedV2> & V2 //Eigen const hack not necessary for the example
) 
{
  copy(V1.row(0), V2.row(0)) ;
}

现在我想要复制功能的通用性。

//generic without constness,deduction fails with a template function as a parameter
template<
  typename DerivedV1,
  typename DerivedV2,
  typename Callback
  >
void generic
(
  Eigen::MatrixBase<DerivedV1> & V1, //no const here, this is my problem
  Eigen::MatrixBase<DerivedV2> & V2,
  Callback callback
) 
{
  callback(V1.row(0), V2.row(0)) ;
}

但是参数自动推导模板不会推导回调的模板参数。到目前为止,我找到的唯一解决方案是这种丑陋的过载,我并不为此感到自豪,并欢迎提出建议。

//overload to help template deduction
template<
  typename DerivedV1,
  typename DerivedV2
  >
void generic
(
  Eigen::MatrixBase<DerivedV1> & V1,
  Eigen::MatrixBase<DerivedV2> & V2,
  void(*callback)(
    const Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV1> &>().row(0)) > &, 
    Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV2> &>().row(0)) > const &
  )
) 
{
  generic<
    DerivedV1, 
    DerivedV2, 
    void(*)(
      const Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV1> &>().row(0)) > &, 
      Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV2> &>().row(0)) > const &
    )
    >(V1, V2, callback) ;
}

这行得通,但我无法在 generic 的第一个参数中处理 const

//desired generic with the const parameter
template<
  typename DerivedV1,
  typename DerivedV2,
  typename Callback
  >
void generic_const
(
  const Eigen::MatrixBase<DerivedV1> & V1, //here is the desired const
  Eigen::MatrixBase<DerivedV2> & V2,
  Callback callback
) 
{
  callback(V1.row(0), V2.row(0)) ;
}

//attempt for an overload to help template deduction
template<
  typename DerivedV1,
  typename DerivedV2
  >
void generic_const
(
  const Eigen::MatrixBase<DerivedV1> & V1,
  Eigen::MatrixBase<DerivedV2> & V2,
  void(*callback)(
    const Eigen::MatrixBase< decltype(std::declval< const Eigen::MatrixBase<DerivedV1> &>().row(0)) > &, 
    Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV2> &>().row(0)) > const &
  )
) 
{
  generic_const<
    DerivedV1, 
    DerivedV2, 
    void(*)(
      const Eigen::MatrixBase< decltype(std::declval< const Eigen::MatrixBase<DerivedV1> &>().row(0)) > &, 
      Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV2> &>().row(0)) > const &
    )
    >(V1, V2, callback) ;
}

int main() {
  Eigen::Matrix3f m1 ;
  m1.setRandom() ;

  Eigen::Matrix3f m2 ;
  m2.setRandom() ;

  std::cout << "== random m1 ==" << std::endl << m1 << std::endl ;
  std::cout << "== random m2 ==" << std::endl << m2 << std::endl ;

  reference(m1, m2) ;

  std::cout << "== reference behaviour ==" << std::endl << m2 << std::endl ;

  m2.setRandom() ;

  std::cout << "== random m2 ==" << std::endl << m2 << std::endl ;

  //this is ok
  generic(m1, m2, copy) ;
  //this will not compile

  std::cout << "== final m2 ==" << std::endl << m2 << std::endl ;

  return 0 ;
}

这里是错误

error: invalid initialization of reference of type 
‘const Eigen::MatrixBase<const Eigen::Block<const Eigen::Matrix<float, 3, 3>, 1, 3, false> >&’ 
from expression of type 
‘Eigen::DenseBase<Eigen::Matrix<float, 3, 3> >::ConstRowXpr’ 
{aka ‘const Eigen::Block<const Eigen::Matrix<float, 3, 3>, 1, 3, false>’}

完整示例在以下代码段中。

#include <eigen3/Eigen/Dense>
#include <iostream>
#include <utility>

//a generic function copying a matrix into another
template<
  typename DerivedV1,
  typename DerivedV2
  >
void copy
(
  const Eigen::MatrixBase<DerivedV1> & v1,
  Eigen::MatrixBase<DerivedV2> const & v2 //Eigen const hack, this is not const
)
{
  Eigen::MatrixBase<DerivedV2> & v2_cast = 
    const_cast< Eigen::MatrixBase<DerivedV2> & >(v2) ;
  v2_cast = v1 ;
}

//reference non generic behaviour
template<
  typename DerivedV1,
  typename DerivedV2
  >
void reference
(
  const Eigen::MatrixBase<DerivedV1> & V1,
  Eigen::MatrixBase<DerivedV2> & V2 //Eigen const hack is not necessary in this example
) 
{
  copy(V1.row(0), V2.row(0)) ;
}

//generic without constness,deduction fails with a template function as a parameter
template<
  typename DerivedV1,
  typename DerivedV2,
  typename Callback
  >
void generic
(
  Eigen::MatrixBase<DerivedV1> & V1, //no const here this is my problem
  Eigen::MatrixBase<DerivedV2> & V2,
  Callback callback
) 
{
  callback(V1.row(0), V2.row(0)) ;
}

//overload to help template deduction
template<
  typename DerivedV1,
  typename DerivedV2
  >
void generic
(
  Eigen::MatrixBase<DerivedV1> & V1,
  Eigen::MatrixBase<DerivedV2> & V2,
  void(*callback)(
    const Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV1> &>().row(0)) > &, 
    Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV2> &>().row(0)) > const &
  )
) 
{
  generic<
    DerivedV1, 
    DerivedV2, 
    void(*)(
      const Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV1> &>().row(0)) > &, 
      Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV2> &>().row(0)) > const &
    )
    >(V1, V2, callback) ;
}

//desired generic with the const parameter
template<
  typename DerivedV1,
  typename DerivedV2,
  typename Callback
  >
void generic_const
(
  const Eigen::MatrixBase<DerivedV1> & V1,
  Eigen::MatrixBase<DerivedV2> & V2,
  Callback callback
) 
{
  callback(V1.row(0), V2.row(0)) ;
}

//attempt for an overload to help template deduction
template<
  typename DerivedV1,
  typename DerivedV2
  >
void generic_const
(
  const Eigen::MatrixBase<DerivedV1> & V1,
  Eigen::MatrixBase<DerivedV2> & V2,
  void(*callback)(
    const Eigen::MatrixBase< decltype(std::declval< const Eigen::MatrixBase<DerivedV1> &>().row(0)) > &, 
    Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV2> &>().row(0)) > const &
  )
) 
{
  generic_const<
    DerivedV1, 
    DerivedV2, 
    void(*)(
      const Eigen::MatrixBase< decltype(std::declval< const Eigen::MatrixBase<DerivedV1> &>().row(0)) > &, 
      Eigen::MatrixBase< decltype(std::declval< Eigen::MatrixBase<DerivedV2> &>().row(0)) > const &
    )
    >(V1, V2, callback) ;
}

int main() {
  Eigen::Matrix3f m1 ;
  m1.setRandom() ;

  Eigen::Matrix3f m2 ;
  m2.setRandom() ;

  std::cout << "== random m1 ==" << std::endl << m1 << std::endl ;
  std::cout << "== random m2 ==" << std::endl << m2 << std::endl ;

  reference(m1, m2) ;

  std::cout << "== reference behaviour ==" << std::endl << m2 << std::endl ;

  m2.setRandom() ;

  std::cout << "== random m2 ==" << std::endl << m2 << std::endl ;

  generic(m1, m2, copy) ;
  generic_const(m1, m2, copy) ;

  std::cout << "== final m2 ==" << std::endl << m2 << std::endl ;

  return 0 ;
}

要处理重载,传递函子而不是函数,例如:

auto copy_functor = [](auto&& source, auto&& dest){ return copy(source, dest); }

generic(m1, m2, copy_functor);
generic_const(m1, m2, copy_functor);