This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen
Bug 968 - Add compatibility with tuple_size
Summary: Add compatibility with tuple_size
Status: DECISIONNEEDED
Alias: None
Product: Eigen
Classification: Unclassified
Component: Interoperability (show other bugs)
Version: 3.3 (current stable)
Hardware: All All
: Normal Feature Request
Assignee: Nobody
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-02-19 22:55 UTC by Ilja Honkonen
Modified: 2019-12-04 14:18 UTC (History)
5 users (show)



Attachments

Description Ilja Honkonen 2015-02-19 22:55:59 UTC
For fixed size Eigen containers it would be nice to be able to get the size in a way compatible with std versions i.e. tuple_size:
namespace std {
	template<> class tuple_size<Eigen::Vector2d> { public: static const size_t value = 2; };
	template<> class tuple_size<Eigen::Vector3d> { public: static const size_t value = 3; };
	...
}

The above can probably be written in a more generic way. Additionally/alternatively .size() should be constexpr as it is in std::array.
Comment 1 Ilja Honkonen 2015-02-19 23:08:45 UTC
This seems to work for all cases:
namespace std {
	template <
		typename _Scalar,
		int _Rows, int _Cols,
		int _Options,
		int _MaxRows, int _MaxCols
	> class tuple_size<
		Eigen::Matrix<
			_Scalar,
			_Rows, _Cols,
			_Options,
			_MaxRows, _MaxCols
		>
	> {
	public:
		static_assert(
			_Rows != Eigen::Dynamic and _Cols != Eigen::Dynamic,
			"tuple_size is only supported for fixed size matrices"
		);
		static const size_t value = _Rows * _Cols;
	};
}
Comment 2 Christoph Hertzberg 2015-02-19 23:23:49 UTC
If we want this, this should be possible by using the already existing SizeAtCompileTime. Something like this (not tested):

  template<typename Derived>
  struct tuple_size<EigenBase<Derived> >{
    static const size_t value = Derived::SizeAtCompileTime;
  };

With our current implementation we must put static_asserts inside code blocks (cf. Bug 742), and we can't use arbitrary strings.
So we could silently return size_t(-1)==MAX_LONG or find another way to disable this (e.g. template specialization).
Comment 3 Ilja Honkonen 2015-02-20 16:42:49 UTC
>   template<typename Derived>
>   struct tuple_size<EigenBase<Derived> >{
>     static const size_t value = Derived::SizeAtCompileTime;
>   };
> With our current implementation we must put static_asserts inside code
> blocks (cf. Bug 742), and we can't use arbitrary strings.
> So we could silently return size_t(-1)==MAX_LONG or find another way to
> disable this (e.g. template specialization).

For resizable containers value should probably be zero as storage for anything is not guaranteed to exist. std::array (and vector naturally) can also have 0 size in which case begin() == end() and data() points to something sane but front() and back() are undefined. The dynamic case(s) could be different specializations that always return 0 so no asserts would be needed.
Comment 4 Nobody 2019-12-04 14:18:05 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/968.

Note You need to log in before you can comment on or make changes to this bug.