C++ eigen静态断言使枚举与浮子混淆

发布于 2025-02-06 15:08:58 字数 2115 浏览 1 评论 0原文

这是代码:

#include <Eigen/Dense>
using namespace Eigen;
enum { TWO = 2};
Matrix<float, Dynamic, TWO> m1(42, 2); // good...
Matrix<float, Dynamic, TWO> m2(42, int(TWO)); // good...
Matrix<float, Dynamic, TWO> m3(42, TWO); // error!

当我编译时,M3的声明会引起错误,好像枚举值两个是浮点数:

$ g++ -I/usr/include/eigen3 -c bug.cpp
In file included from /usr/include/eigen3/Eigen/Core:366,
                 from /usr/include/eigen3/Eigen/Dense:1,
                 from bug.cpp:1:
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h: In instantiation of ‘void Eigen::PlainObjectBase<Derived>::_init2(Eigen::Index, Eigen::Index, typename Eigen::internal::enable_if<(typename Eigen::internal::dense_xpr_base<Derived>::type::SizeAtCompileTime != 2), T0>::type*) [with T0 = int; T1 = <unnamed enum>; Derived = Eigen::Matrix<float, -1, 2>; Eigen::Index = long int; typename Eigen::internal::enable_if<(typename Eigen::internal::dense_xpr_base<Derived>::type::SizeAtCompileTime != 2), T0>::type = int]’:
/usr/include/eigen3/Eigen/src/Core/Matrix.h:302:35:   required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const T0&, const T1&) [with T0 = int; T1 = <unnamed enum>; _Scalar = float; int _Rows = -1; int _Cols = 2; int _Options = 0; int _MaxRows = -1; int _MaxCols = 2]’
bug.cpp:6:39:   required from here
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:740:58: error: static assertion failed: FLOATING_POINT_ARGUMENT_PASSED__INTEGER_WAS_EXPECTED
  740 |       EIGEN_STATIC_ASSERT(bool(NumTraits<T0>::IsInteger) &&
      |                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
  741 |                           bool(NumTraits<T1>::IsInteger),
      |                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
/usr/include/eigen3/Eigen/src/Core/util/StaticAssert.h:33:54: note: in definition of macro ‘EIGEN_STATIC_ASSERT’
   33 |     #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
      |                       

Here is the code:

#include <Eigen/Dense>
using namespace Eigen;
enum { TWO = 2};
Matrix<float, Dynamic, TWO> m1(42, 2); // good...
Matrix<float, Dynamic, TWO> m2(42, int(TWO)); // good...
Matrix<float, Dynamic, TWO> m3(42, TWO); // error!

When I compile the declaration of m3 is raising an error as if the enum value TWO is a floating point number:

$ g++ -I/usr/include/eigen3 -c bug.cpp
In file included from /usr/include/eigen3/Eigen/Core:366,
                 from /usr/include/eigen3/Eigen/Dense:1,
                 from bug.cpp:1:
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h: In instantiation of ‘void Eigen::PlainObjectBase<Derived>::_init2(Eigen::Index, Eigen::Index, typename Eigen::internal::enable_if<(typename Eigen::internal::dense_xpr_base<Derived>::type::SizeAtCompileTime != 2), T0>::type*) [with T0 = int; T1 = <unnamed enum>; Derived = Eigen::Matrix<float, -1, 2>; Eigen::Index = long int; typename Eigen::internal::enable_if<(typename Eigen::internal::dense_xpr_base<Derived>::type::SizeAtCompileTime != 2), T0>::type = int]’:
/usr/include/eigen3/Eigen/src/Core/Matrix.h:302:35:   required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const T0&, const T1&) [with T0 = int; T1 = <unnamed enum>; _Scalar = float; int _Rows = -1; int _Cols = 2; int _Options = 0; int _MaxRows = -1; int _MaxCols = 2]’
bug.cpp:6:39:   required from here
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:740:58: error: static assertion failed: FLOATING_POINT_ARGUMENT_PASSED__INTEGER_WAS_EXPECTED
  740 |       EIGEN_STATIC_ASSERT(bool(NumTraits<T0>::IsInteger) &&
      |                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
  741 |                           bool(NumTraits<T1>::IsInteger),
      |                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
/usr/include/eigen3/Eigen/src/Core/util/StaticAssert.h:33:54: note: in definition of macro ‘EIGEN_STATIC_ASSERT’
   33 |     #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
      |                       

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2025-02-13 15:08:58

错误消息只是选择不正确,因为它总是将浮点提及引起问题,但要点是您需要通过整数类型,而枚举类型不是。

但是,使用EIGEN版本3.3.8,这已更改,现在也接受了枚举类型,并将转换为其基础整数类型,请参见此问题导致 。由于该版本您的代码填充了,因此请参见 https://godbolt.org/z/w4jybkfns

The error message is just not chosen well since it always mentions floating points as causing the issue, but the point is that you need to pass an integer type, which an enumeration type is not.

However, with Eigen version 3.3.8 this has been changed and an enumeration type is now accepted as well and will be converted to its underlying integer type, see this issue resulting in this commit. Since that version your code compiles fine, see https://godbolt.org/z/W4jYbKfns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文