From 36448c9e287935b8c408791bf88b2907292d6c80 Mon Sep 17 00:00:00 2001 From: Christoph Hertzberg Date: Tue, 23 Sep 2014 14:28:23 +0200 Subject: Make constructors explicit if they could lead to unintended implicit conversion --- Eigen/src/Core/VectorwiseOp.h | 88 +++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 36 deletions(-) (limited to 'Eigen/src/Core/VectorwiseOp.h') diff --git a/Eigen/src/Core/VectorwiseOp.h b/Eigen/src/Core/VectorwiseOp.h index a8130e902..e340c1433 100644 --- a/Eigen/src/Core/VectorwiseOp.h +++ b/Eigen/src/Core/VectorwiseOp.h @@ -65,7 +65,7 @@ class PartialReduxExpr : internal::no_assignment_operator, typedef typename internal::traits::MatrixTypeNested MatrixTypeNested; typedef typename internal::traits::_MatrixTypeNested _MatrixTypeNested; - PartialReduxExpr(const MatrixType& mat, const MemberOp& func = MemberOp()) + explicit PartialReduxExpr(const MatrixType& mat, const MemberOp& func = MemberOp()) : m_matrix(mat), m_functor(func) {} Index rows() const { return (Direction==Vertical ? 1 : m_matrix.rows()); } @@ -128,7 +128,7 @@ struct member_redux { >::type result_type; template struct Cost { enum { value = (Size-1) * functor_traits::Cost }; }; - member_redux(const BinaryOp func) : m_functor(func) {} + member_redux(const BinaryOp func) : m_functor(func) {} // FIXME this should actually be explicit, but lets not exaggerate template inline result_type operator()(const DenseBase& mat) const { return mat.redux(m_functor); } @@ -249,7 +249,7 @@ template class VectorwiseOp public: - inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {} + explicit inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {} /** \internal */ inline const ExpressionType& _expression() const { return m_matrix; } @@ -266,6 +266,21 @@ template class VectorwiseOp redux(const BinaryOp& func = BinaryOp()) const { return typename ReduxReturnType::Type(_expression(), func); } + typedef typename ReturnType::Type MinCoeffReturnType; + typedef typename ReturnType::Type MaxCoeffReturnType; + typedef typename ReturnType::Type SquareNormReturnType; + typedef typename ReturnType::Type NormReturnType; + typedef typename ReturnType::Type BlueNormReturnType; + typedef typename ReturnType::Type StableNormReturnType; + typedef typename ReturnType::Type HypotNormReturnType; + typedef typename ReturnType::Type SumReturnType; + typedef typename ReturnType::Type MeanReturnType; + typedef typename ReturnType::Type AllReturnType; + typedef typename ReturnType::Type AnyReturnType; + typedef PartialReduxExpr, Direction> CountReturnType; + typedef typename ReturnType::Type ProdReturnType; + typedef Reverse ReverseReturnType; + /** \returns a row (or column) vector expression of the smallest coefficient * of each column (or row) of the referenced expression. * @@ -275,8 +290,8 @@ template class VectorwiseOp * Output: \verbinclude PartialRedux_minCoeff.out * * \sa DenseBase::minCoeff() */ - const typename ReturnType::Type minCoeff() const - { return _expression(); } + const MinCoeffReturnType minCoeff() const + { return MinCoeffReturnType(_expression()); } /** \returns a row (or column) vector expression of the largest coefficient * of each column (or row) of the referenced expression. @@ -287,8 +302,8 @@ template class VectorwiseOp * Output: \verbinclude PartialRedux_maxCoeff.out * * \sa DenseBase::maxCoeff() */ - const typename ReturnType::Type maxCoeff() const - { return _expression(); } + const MaxCoeffReturnType maxCoeff() const + { return MaxCoeffReturnType(_expression()); } /** \returns a row (or column) vector expression of the squared norm * of each column (or row) of the referenced expression. @@ -298,8 +313,8 @@ template class VectorwiseOp * Output: \verbinclude PartialRedux_squaredNorm.out * * \sa DenseBase::squaredNorm() */ - const typename ReturnType::Type squaredNorm() const - { return _expression(); } + const SquareNormReturnType squaredNorm() const + { return SquareNormReturnType(_expression()); } /** \returns a row (or column) vector expression of the norm * of each column (or row) of the referenced expression. @@ -309,8 +324,8 @@ template class VectorwiseOp * Output: \verbinclude PartialRedux_norm.out * * \sa DenseBase::norm() */ - const typename ReturnType::Type norm() const - { return _expression(); } + const NormReturnType norm() const + { return NormReturnType(_expression()); } /** \returns a row (or column) vector expression of the norm @@ -319,8 +334,8 @@ template class VectorwiseOp * This is a vector with real entries, even if the original matrix has complex entries. * * \sa DenseBase::blueNorm() */ - const typename ReturnType::Type blueNorm() const - { return _expression(); } + const BlueNormReturnType blueNorm() const + { return BlueNormReturnType(_expression()); } /** \returns a row (or column) vector expression of the norm @@ -329,8 +344,8 @@ template class VectorwiseOp * This is a vector with real entries, even if the original matrix has complex entries. * * \sa DenseBase::stableNorm() */ - const typename ReturnType::Type stableNorm() const - { return _expression(); } + const StableNormReturnType stableNorm() const + { return StableNormReturnType(_expression()); } /** \returns a row (or column) vector expression of the norm @@ -339,8 +354,8 @@ template class VectorwiseOp * This is a vector with real entries, even if the original matrix has complex entries. * * \sa DenseBase::hypotNorm() */ - const typename ReturnType::Type hypotNorm() const - { return _expression(); } + const HypotNormReturnType hypotNorm() const + { return HypotNormReturnType(_expression()); } /** \returns a row (or column) vector expression of the sum * of each column (or row) of the referenced expression. @@ -349,31 +364,31 @@ template class VectorwiseOp * Output: \verbinclude PartialRedux_sum.out * * \sa DenseBase::sum() */ - const typename ReturnType::Type sum() const - { return _expression(); } + const SumReturnType sum() const + { return SumReturnType(_expression()); } /** \returns a row (or column) vector expression of the mean * of each column (or row) of the referenced expression. * * \sa DenseBase::mean() */ - const typename ReturnType::Type mean() const - { return _expression(); } + const MeanReturnType mean() const + { return MeanReturnType(_expression()); } /** \returns a row (or column) vector expression representing * whether \b all coefficients of each respective column (or row) are \c true. * This expression can be assigned to a vector with entries of type \c bool. * * \sa DenseBase::all() */ - const typename ReturnType::Type all() const - { return _expression(); } + const AllReturnType all() const + { return AllReturnType(_expression()); } /** \returns a row (or column) vector expression representing * whether \b at \b least one coefficient of each respective column (or row) is \c true. * This expression can be assigned to a vector with entries of type \c bool. * * \sa DenseBase::any() */ - const typename ReturnType::Type any() const - { return _expression(); } + const AnyReturnType any() const + { return Any(_expression()); } /** \returns a row (or column) vector expression representing * the number of \c true coefficients of each respective column (or row). @@ -384,8 +399,8 @@ template class VectorwiseOp * Output: \verbinclude PartialRedux_count.out * * \sa DenseBase::count() */ - const PartialReduxExpr, Direction> count() const - { return _expression(); } + const CountReturnType count() const + { return CountReturnType(_expression()); } /** \returns a row (or column) vector expression of the product * of each column (or row) of the referenced expression. @@ -394,8 +409,8 @@ template class VectorwiseOp * Output: \verbinclude PartialRedux_prod.out * * \sa DenseBase::prod() */ - const typename ReturnType::Type prod() const - { return _expression(); } + const ProdReturnType prod() const + { return ProdReturnType(_expression()); } /** \returns a matrix expression @@ -405,8 +420,8 @@ template class VectorwiseOp * Output: \verbinclude Vectorwise_reverse.out * * \sa DenseBase::reverse() */ - const Reverse reverse() const - { return Reverse( _expression() ); } + const ReverseReturnType reverse() const + { return ReverseReturnType( _expression() ); } typedef Replicate ReplicateReturnType; const ReplicateReturnType replicate(Index factor) const; @@ -550,7 +565,8 @@ template class VectorwiseOp /////////// Geometry module /////////// - Homogeneous homogeneous() const; + typedef Homogeneous HomogeneousReturnType; + HomogeneousReturnType homogeneous() const; typedef typename ExpressionType::PlainObject CrossReturnType; template @@ -595,7 +611,7 @@ template inline const typename DenseBase::ConstColwiseReturnType DenseBase::colwise() const { - return derived(); + return ConstColwiseReturnType(derived()); } /** \returns a writable VectorwiseOp wrapper of *this providing additional partial reduction operations @@ -606,7 +622,7 @@ template inline typename DenseBase::ColwiseReturnType DenseBase::colwise() { - return derived(); + return ColwiseReturnType(derived()); } /** \returns a VectorwiseOp wrapper of *this providing additional partial reduction operations @@ -620,7 +636,7 @@ template inline const typename DenseBase::ConstRowwiseReturnType DenseBase::rowwise() const { - return derived(); + return ConstRowwiseReturnType(derived()); } /** \returns a writable VectorwiseOp wrapper of *this providing additional partial reduction operations @@ -631,7 +647,7 @@ template inline typename DenseBase::RowwiseReturnType DenseBase::rowwise() { - return derived(); + return RowwiseReturnType(derived()); } } // end namespace Eigen -- cgit v1.2.3