以Eigen类型为参数的泛型函数回调

时间:2018-09-13 15:45:45

标签: c++ eigen

我正在尝试使用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,但我正在尝试关注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) ;
}

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

//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 ;
}

0 个答案:

没有答案