New user self-registration is currently disabled. Please email eigen-core-team @ if you need an account.
Bug 1222 - AutoDiffScalar doesn't work with operator+ returning ADS<DerType&>
AutoDiffScalar doesn't work with operator+ returning ADS<DerType&>
Product: Eigen
Classification: Unclassified
Component: Unsupported modules
3.3 (current stable)
All All
: Normal Unknown
Assigned To: Nobody
Depends on:
  Show dependency treegraph
Reported: 2016-05-10 10:50 UTC by Daniel Vollmer
Modified: 2016-05-18 14:17 UTC (History)
2 users (show)


Description Daniel Vollmer 2016-05-10 10:50:01 UTC
The following example fails to compile:

#include "Eigen/Core"
#include "unsupported/Eigen/AutoDiff"

int main (int argc, char const *argv[])
  typedef Eigen::AutoDiffScalar<Eigen::Vector3d> AD;
  const double _cv1_3 = 1.0;
  const AD chi_3 = 1.0;
  // this line doesn't work, because operator+ returns ADS<DerType&>, which then cannot be converted to ADS<DerType>
  const AD denom = chi_3 + _cv1_3;
  return 0;

I'm not sure what the intent is for operator+ (and -) to return ADS<DerType&> (probably to reuse the original derivatives?), but then the conversion from ADS<DerType&> to ADS<DerType> doesn't seem allowed, the intended constructor fails due to the unexpected reference in OtherDerType.
Comment 1 Daniel Vollmer 2016-05-10 15:01:21 UTC
Applying the following patch seems to work around the problem for me:

--- a/AutoDiffScalar.h	2016-05-10 04:24:55.000000000 +0200
+++ b/AutoDiffScalar.h	2016-05-10 16:47:36.000000000 +0200
@@ -101,7 +101,7 @@
     template<typename OtherDerType>
     AutoDiffScalar(const AutoDiffScalar<OtherDerType>& other
-    , typename internal::enable_if<internal::is_same<Scalar,typename OtherDerType::Scalar>::value,void*>::type = 0
+    , typename internal::enable_if<internal::is_same<Scalar, typename Eigen::internal::traits<typename Eigen::internal::remove_all<DerType>::type>::Scalar>::value,void*>::type = 0
       : m_value(other.value()), m_derivatives(other.derivatives())
Comment 2 Gael Guennebaud 2016-05-18 14:04:20 UTC
Thanks for the patch, but it should be OtherDerType instead of DerType. Also, this issue applies to 3.3 only.

Backport of the unit test to 3.2:
Comment 3 Daniel Vollmer 2016-05-18 14:17:08 UTC
Ah yes, I had taken the most recent AutoDiffScalar from the default branch to get the fix to which might be why I saw this problem using an otherwise vanilla Eigen 3.2.


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