Make constructors explicit if they could lead to unintended implicit conversion

This commit is contained in:
Christoph Hertzberg
2014-09-23 14:28:23 +02:00
parent de0d8a010e
commit 36448c9e28
93 changed files with 416 additions and 355 deletions

View File

@@ -1,5 +1,24 @@
// These are already defined in MatrixCwiseUnaryOps.h
//typedef CwiseUnaryOp<internal::scalar_abs_op<Scalar>, const Derived> AbsReturnType;
//typedef CwiseUnaryOp<internal::scalar_abs2_op<Scalar>, const Derived> Abs2ReturnType;
//typedef CwiseUnaryOp<internal::scalar_sqrt_op<Scalar>, const Derived> SqrtReturnType;
//typedef CwiseUnaryOp<internal::scalar_inverse_op<Scalar>, const Derived> CwiseInverseReturnType;
//typedef CwiseUnaryOp<std::binder1st<std::equal_to<Scalar> >, const Derived> CwiseScalarEqualReturnType;
typedef CwiseUnaryOp<internal::scalar_exp_op<Scalar>, const Derived> ExpReturnType;
typedef CwiseUnaryOp<internal::scalar_log_op<Scalar>, const Derived> LogReturnType;
typedef CwiseUnaryOp<internal::scalar_cos_op<Scalar>, const Derived> CosReturnType;
typedef CwiseUnaryOp<internal::scalar_sin_op<Scalar>, const Derived> SinReturnType;
typedef CwiseUnaryOp<internal::scalar_acos_op<Scalar>, const Derived> AcosReturnType;
typedef CwiseUnaryOp<internal::scalar_asin_op<Scalar>, const Derived> AsinReturnType;
typedef CwiseUnaryOp<internal::scalar_tan_op<Scalar>, const Derived> TanReturnType;
typedef CwiseUnaryOp<internal::scalar_atan_op<Scalar>, const Derived> AtanReturnType;
typedef CwiseUnaryOp<internal::scalar_pow_op<Scalar>, const Derived> PowReturnType;
typedef CwiseUnaryOp<internal::scalar_square_op<Scalar>, const Derived> SquareReturnType;
typedef CwiseUnaryOp<internal::scalar_cube_op<Scalar>, const Derived> CubeReturnType;
/** \returns an expression of the coefficient-wise absolute value of \c *this
*
* Example: \include Cwise_abs.cpp
@@ -8,10 +27,10 @@
* \sa abs2()
*/
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE const CwiseUnaryOp<internal::scalar_abs_op<Scalar>, const Derived>
EIGEN_STRONG_INLINE const AbsReturnType
abs() const
{
return derived();
return AbsReturnType(derived());
}
/** \returns an expression of the coefficient-wise squared absolute value of \c *this
@@ -22,10 +41,10 @@ abs() const
* \sa abs(), square()
*/
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE const CwiseUnaryOp<internal::scalar_abs2_op<Scalar>, const Derived>
EIGEN_STRONG_INLINE const Abs2ReturnType
abs2() const
{
return derived();
return Abs2ReturnType(derived());
}
/** \returns an expression of the coefficient-wise exponential of *this.
@@ -39,10 +58,10 @@ abs2() const
* \sa pow(), log(), sin(), cos()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_exp_op<Scalar>, const Derived>
inline const ExpReturnType
exp() const
{
return derived();
return ExpReturnType(derived());
}
/** \returns an expression of the coefficient-wise logarithm of *this.
@@ -56,10 +75,10 @@ exp() const
* \sa exp()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_log_op<Scalar>, const Derived>
inline const LogReturnType
log() const
{
return derived();
return LogReturnType(derived());
}
/** \returns an expression of the coefficient-wise square root of *this.
@@ -73,10 +92,10 @@ log() const
* \sa pow(), square()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_sqrt_op<Scalar>, const Derived>
inline const SqrtReturnType
sqrt() const
{
return derived();
return SqrtReturnType(derived());
}
/** \returns an expression of the coefficient-wise cosine of *this.
@@ -90,10 +109,10 @@ sqrt() const
* \sa sin(), acos()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_cos_op<Scalar>, const Derived>
inline const CosReturnType
cos() const
{
return derived();
return CosReturnType(derived());
}
@@ -108,10 +127,10 @@ cos() const
* \sa cos(), asin()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_sin_op<Scalar>, const Derived>
inline const SinReturnType
sin() const
{
return derived();
return SinReturnType(derived());
}
/** \returns an expression of the coefficient-wise arc cosine of *this.
@@ -122,10 +141,10 @@ sin() const
* \sa cos(), asin()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_acos_op<Scalar>, const Derived>
inline const AcosReturnType
acos() const
{
return derived();
return AcosReturnType(derived());
}
/** \returns an expression of the coefficient-wise arc sine of *this.
@@ -136,10 +155,10 @@ acos() const
* \sa sin(), acos()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_asin_op<Scalar>, const Derived>
inline const AsinReturnType
asin() const
{
return derived();
return AsinReturnType(derived());
}
/** \returns an expression of the coefficient-wise tan of *this.
@@ -150,10 +169,10 @@ asin() const
* \sa cos(), sin()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_tan_op<Scalar>, Derived>
inline const TanReturnType
tan() const
{
return derived();
return TanReturnType(derived());
}
/** \returns an expression of the coefficient-wise arc tan of *this.
@@ -163,10 +182,10 @@ tan() const
*
* \sa cos(), sin(), tan()
*/
inline const CwiseUnaryOp<internal::scalar_atan_op<Scalar>, Derived>
inline const AtanReturnType
atan() const
{
return derived();
return AtanReturnType(derived());
}
/** \returns an expression of the coefficient-wise power of *this to the given exponent.
@@ -180,11 +199,10 @@ atan() const
* \sa exp(), log()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_pow_op<Scalar>, const Derived>
inline const PowReturnType
pow(const Scalar& exponent) const
{
return CwiseUnaryOp<internal::scalar_pow_op<Scalar>, const Derived>
(derived(), internal::scalar_pow_op<Scalar>(exponent));
return PowReturnType(derived(), internal::scalar_pow_op<Scalar>(exponent));
}
@@ -196,10 +214,10 @@ pow(const Scalar& exponent) const
* \sa operator/(), operator*()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_inverse_op<Scalar>, const Derived>
inline const CwiseInverseReturnType
inverse() const
{
return derived();
return CwiseInverseReturnType(derived());
}
/** \returns an expression of the coefficient-wise square of *this.
@@ -210,10 +228,10 @@ inverse() const
* \sa operator/(), operator*(), abs2()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_square_op<Scalar>, const Derived>
inline const SquareReturnType
square() const
{
return derived();
return SquareReturnType(derived());
}
/** \returns an expression of the coefficient-wise cube of *this.
@@ -224,10 +242,10 @@ square() const
* \sa square(), pow()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_cube_op<Scalar>, const Derived>
inline const CubeReturnType
cube() const
{
return derived();
return CubeReturnType(derived());
}
#define EIGEN_MAKE_SCALAR_CWISE_UNARY_OP(METHOD_NAME,FUNCTOR) \

View File

@@ -14,6 +14,8 @@
/** \internal Represents a scalar multiple of an expression */
typedef CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, const Derived> ScalarMultipleReturnType;
typedef CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >, const Derived> ScalarComplexMultipleReturnType;
/** \internal Represents a quotient of an expression by a scalar*/
typedef CwiseUnaryOp<internal::scalar_quotient1_op<Scalar>, const Derived> ScalarQuotient1ReturnType;
/** \internal the return type of conjugate() */
@@ -36,13 +38,16 @@ typedef CwiseUnaryOp<internal::scalar_imag_op<Scalar>, const Derived> ImagReturn
/** \internal the return type of imag() */
typedef CwiseUnaryView<internal::scalar_imag_ref_op<Scalar>, Derived> NonConstImagReturnType;
typedef CwiseUnaryOp<internal::scalar_opposite_op<Scalar>, const Derived> NegativeReturnType;
//typedef CwiseUnaryOp<internal::scalar_quotient1_op<Scalar>, const Derived>
#endif // not EIGEN_PARSED_BY_DOXYGEN
/** \returns an expression of the opposite of \c *this
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_opposite_op<typename internal::traits<Derived>::Scalar>, const Derived>
operator-() const { return derived(); }
inline const NegativeReturnType
operator-() const { return NegativeReturnType(derived()); }
/** \returns an expression of \c *this scaled by the scalar factor \a scalar */
@@ -50,8 +55,7 @@ EIGEN_DEVICE_FUNC
inline const ScalarMultipleReturnType
operator*(const Scalar& scalar) const
{
return CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, const Derived>
(derived(), internal::scalar_multiple_op<Scalar>(scalar));
return ScalarMultipleReturnType(derived(), internal::scalar_multiple_op<Scalar>(scalar));
}
#ifdef EIGEN_PARSED_BY_DOXYGEN
@@ -60,20 +64,18 @@ const ScalarMultipleReturnType operator*(const RealScalar& scalar) const;
/** \returns an expression of \c *this divided by the scalar value \a scalar */
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_quotient1_op<typename internal::traits<Derived>::Scalar>, const Derived>
inline const ScalarQuotient1ReturnType
operator/(const Scalar& scalar) const
{
return CwiseUnaryOp<internal::scalar_quotient1_op<Scalar>, const Derived>
(derived(), internal::scalar_quotient1_op<Scalar>(scalar));
return ScalarQuotient1ReturnType(derived(), internal::scalar_quotient1_op<Scalar>(scalar));
}
/** Overloaded for efficient real matrix times complex scalar value */
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >, const Derived>
inline const ScalarComplexMultipleReturnType
operator*(const std::complex<Scalar>& scalar) const
{
return CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >, const Derived>
(*static_cast<const Derived*>(this), internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >(scalar));
return ScalarComplexMultipleReturnType(derived(), internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >(scalar));
}
EIGEN_DEVICE_FUNC
@@ -86,6 +88,9 @@ inline friend const CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,std::compl
operator*(const std::complex<Scalar>& scalar, const StorageBaseType& matrix)
{ return matrix*scalar; }
template<class NewType> struct CastXpr { typedef typename internal::cast_return_type<Derived,const CwiseUnaryOp<internal::scalar_cast_op<Scalar, NewType>, const Derived> >::type Type; };
/** \returns an expression of *this with the \a Scalar type casted to
* \a NewScalar.
*
@@ -95,10 +100,10 @@ operator*(const std::complex<Scalar>& scalar, const StorageBaseType& matrix)
*/
template<typename NewType>
EIGEN_DEVICE_FUNC
typename internal::cast_return_type<Derived,const CwiseUnaryOp<internal::scalar_cast_op<typename internal::traits<Derived>::Scalar, NewType>, const Derived> >::type
typename CastXpr<NewType>::Type
cast() const
{
return derived();
return typename CastXpr<NewType>::Type(derived());
}
/** \returns an expression of the complex conjugate of \c *this.
@@ -116,14 +121,14 @@ conjugate() const
* \sa imag() */
EIGEN_DEVICE_FUNC
inline RealReturnType
real() const { return derived(); }
real() const { return RealReturnType(derived()); }
/** \returns an read-only expression of the imaginary part of \c *this.
*
* \sa real() */
EIGEN_DEVICE_FUNC
inline const ImagReturnType
imag() const { return derived(); }
imag() const { return ImagReturnType(derived()); }
/** \brief Apply a unary operator coefficient-wise
* \param[in] func Functor implementing the unary operator
@@ -176,11 +181,11 @@ unaryViewExpr(const CustomViewOp& func = CustomViewOp()) const
* \sa imag() */
EIGEN_DEVICE_FUNC
inline NonConstRealReturnType
real() { return derived(); }
real() { return NonConstRealReturnType(derived()); }
/** \returns a non const expression of the imaginary part of \c *this.
*
* \sa real() */
EIGEN_DEVICE_FUNC
inline NonConstImagReturnType
imag() { return derived(); }
imag() { return NonConstImagReturnType(derived()); }

View File

@@ -10,6 +10,11 @@
// This file is a base class plugin containing matrix specifics coefficient wise functions.
typedef CwiseUnaryOp<internal::scalar_abs_op<Scalar>, const Derived> AbsReturnType;
typedef CwiseUnaryOp<internal::scalar_abs2_op<Scalar>, const Derived> Abs2ReturnType;
typedef CwiseUnaryOp<internal::scalar_sqrt_op<Scalar>, const Derived> SqrtReturnType;
typedef CwiseUnaryOp<internal::scalar_inverse_op<Scalar>, const Derived> CwiseInverseReturnType;
typedef CwiseUnaryOp<std::binder1st<std::equal_to<Scalar> >, const Derived> CwiseScalarEqualReturnType;
/** \returns an expression of the coefficient-wise absolute value of \c *this
*
* Example: \include MatrixBase_cwiseAbs.cpp
@@ -18,8 +23,8 @@
* \sa cwiseAbs2()
*/
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE const CwiseUnaryOp<internal::scalar_abs_op<Scalar>, const Derived>
cwiseAbs() const { return derived(); }
EIGEN_STRONG_INLINE const AbsReturnType
cwiseAbs() const { return AbsReturnType(derived()); }
/** \returns an expression of the coefficient-wise squared absolute value of \c *this
*
@@ -29,8 +34,8 @@ cwiseAbs() const { return derived(); }
* \sa cwiseAbs()
*/
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE const CwiseUnaryOp<internal::scalar_abs2_op<Scalar>, const Derived>
cwiseAbs2() const { return derived(); }
EIGEN_STRONG_INLINE const Abs2ReturnType
cwiseAbs2() const { return Abs2ReturnType(derived()); }
/** \returns an expression of the coefficient-wise square root of *this.
*
@@ -40,8 +45,8 @@ cwiseAbs2() const { return derived(); }
* \sa cwisePow(), cwiseSquare()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_sqrt_op<Scalar>, const Derived>
cwiseSqrt() const { return derived(); }
inline const SqrtReturnType
cwiseSqrt() const { return SqrtReturnType(derived()); }
/** \returns an expression of the coefficient-wise inverse of *this.
*
@@ -51,8 +56,8 @@ cwiseSqrt() const { return derived(); }
* \sa cwiseProduct()
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<internal::scalar_inverse_op<Scalar>, const Derived>
cwiseInverse() const { return derived(); }
inline const CwiseInverseReturnType
cwiseInverse() const { return CwiseInverseReturnType(derived()); }
/** \returns an expression of the coefficient-wise == operator of \c *this and a scalar \a s
*
@@ -64,9 +69,8 @@ cwiseInverse() const { return derived(); }
* \sa cwiseEqual(const MatrixBase<OtherDerived> &) const
*/
EIGEN_DEVICE_FUNC
inline const CwiseUnaryOp<std::binder1st<std::equal_to<Scalar> >, const Derived>
inline const CwiseScalarEqualReturnType
cwiseEqual(const Scalar& s) const
{
return CwiseUnaryOp<std::binder1st<std::equal_to<Scalar> >,const Derived>
(derived(), std::bind1st(std::equal_to<Scalar>(), s));
return CwiseScalarEqualReturnType(derived(), std::bind1st(std::equal_to<Scalar>(), s));
}