Skip to content

Commit 32c7768

Browse files
committed
Only allow fvar constructor for convertible types
Only test quadrature constexpr if compatibility macro typo Fix derivative casting? different way boost::has_right_shift fails for me even before fvar changes... prep for PR cleaning
1 parent d721493 commit 32c7768

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

include/boost/math/differentiation/autodiff.hpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ class fvar {
145145
// RealType(ca) | RealType | RealType is copy constructible from the arithmetic types.
146146
explicit fvar(root_type const&); // Initialize a constant. (No epsilon terms.)
147147

148-
template <typename RealType2>
148+
template <typename RealType2, typename std::enable_if<std::is_convertible<RealType2, RealType>::value>::type* = nullptr>
149149
fvar(RealType2 const& ca); // Supports any RealType2 for which static_cast<root_type>(ca) compiles.
150150

151151
// r = cr | RealType& | Assignment operator.
152152
fvar& operator=(fvar const&) = default;
153153

154154
// r = ca | RealType& | Assignment operator from the arithmetic types.
155-
// Handled by constructor that takes a single parameter of generic type.
156-
// fvar& operator=(root_type const&); // Set a constant.
155+
template<typename RealType2>
156+
typename std::enable_if<std::is_convertible<RealType2, RealType>::value, fvar&>::type& operator=(RealType2 const&);
157157

158158
// r += cr | RealType& | Adds cr to r.
159159
template <typename RealType2, size_t Order2>
@@ -678,19 +678,18 @@ fvar<RealType, Order>::fvar(root_type const& ca) : v{{static_cast<RealType>(ca)}
678678

679679
// Can cause compiler error if RealType2 cannot be cast to root_type.
680680
template <typename RealType, size_t Order>
681-
template <typename RealType2>
681+
template <typename RealType2, typename std::enable_if<std::is_convertible<RealType2, RealType>::value>::type*>
682682
fvar<RealType, Order>::fvar(RealType2 const& ca) : v{{static_cast<RealType>(ca)}} {}
683683

684-
/*
685684
template<typename RealType, size_t Order>
686-
fvar<RealType,Order>& fvar<RealType,Order>::operator=(root_type const& ca)
685+
template<typename RealType2>
686+
typename std::enable_if<std::is_convertible<RealType2, RealType>::value, fvar<RealType, Order>&>::type& fvar<RealType,Order>::operator=(RealType2 const& ca)
687687
{
688688
v.front() = static_cast<RealType>(ca);
689-
if constexpr (0 < Order)
689+
BOOST_IF_CONSTEXPR (0 < Order)
690690
std::fill(v.begin()+1, v.end(), static_cast<RealType>(0));
691691
return *this;
692692
}
693-
*/
694693

695694
template <typename RealType, size_t Order>
696695
template <typename RealType2, size_t Order2>

0 commit comments

Comments
 (0)