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

@@ -27,7 +27,7 @@ class AmbiVector
typedef _Index Index;
typedef typename NumTraits<Scalar>::Real RealScalar;
AmbiVector(Index size)
explicit AmbiVector(Index size)
: m_buffer(0), m_zero(0), m_size(0), m_allocatedSize(0), m_allocatedElements(0), m_mode(-1)
{
resize(size);
@@ -288,7 +288,7 @@ class AmbiVector<_Scalar,_Index>::Iterator
* In practice, all coefficients having a magnitude smaller than \a epsilon
* are skipped.
*/
Iterator(const AmbiVector& vec, const RealScalar& epsilon = 0)
explicit Iterator(const AmbiVector& vec, const RealScalar& epsilon = 0)
: m_vector(vec)
{
using std::abs;

View File

@@ -36,7 +36,7 @@ class CompressedStorage
: m_values(0), m_indices(0), m_size(0), m_allocatedSize(0)
{}
CompressedStorage(size_t size)
explicit CompressedStorage(size_t size)
: m_values(0), m_indices(0), m_size(0), m_allocatedSize(0)
{
resize(size);

View File

@@ -192,7 +192,7 @@ struct evaluator<MappedSparseMatrix<_Scalar,_Options,_Index> >
};
evaluator() : m_matrix(0) {}
evaluator(const MappedSparseMatrixType &mat) : m_matrix(&mat) {}
explicit evaluator(const MappedSparseMatrixType &mat) : m_matrix(&mat) {}
operator MappedSparseMatrixType&() { return m_matrix->const_cast_derived(); }
operator const MappedSparseMatrixType&() const { return *m_matrix; }

View File

@@ -443,7 +443,7 @@ namespace internal {
Index m_end;
public:
EIGEN_STRONG_INLINE GenericSparseBlockInnerIteratorImpl(const BlockType& block, Index outer = 0)
explicit EIGEN_STRONG_INLINE GenericSparseBlockInnerIteratorImpl(const BlockType& block, Index outer = 0)
:
m_block(block),
m_outerPos( (IsRowMajor ? block.m_startCol.value() : block.m_startRow.value()) - 1), // -1 so that operator++ finds the first non-zero entry
@@ -512,7 +512,7 @@ struct unary_evaluator<Block<ArgType,BlockRows,BlockCols,InnerPanel>, IteratorBa
typedef typename internal::conditional<OuterVector,OuterVectorInnerIterator,InnerVectorInnerIterator>::type InnerIterator;
unary_evaluator(const XprType& op)
explicit unary_evaluator(const XprType& op)
: m_argImpl(op.nestedExpression()), m_block(op)
{}

View File

@@ -117,7 +117,7 @@ public:
Flags = XprType::Flags
};
binary_evaluator(const XprType& xpr)
explicit binary_evaluator(const XprType& xpr)
: m_functor(xpr.functor()),
m_lhsImpl(xpr.lhs()),
m_rhsImpl(xpr.rhs())
@@ -195,7 +195,7 @@ public:
Flags = XprType::Flags
};
binary_evaluator(const XprType& xpr)
explicit binary_evaluator(const XprType& xpr)
: m_functor(xpr.functor()),
m_lhsImpl(xpr.lhs()),
m_rhsImpl(xpr.rhs())
@@ -260,7 +260,7 @@ public:
Flags = XprType::Flags
};
binary_evaluator(const XprType& xpr)
explicit binary_evaluator(const XprType& xpr)
: m_functor(xpr.functor()),
m_lhsImpl(xpr.lhs()),
m_rhsImpl(xpr.rhs())
@@ -326,7 +326,7 @@ public:
Flags = XprType::Flags
};
binary_evaluator(const XprType& xpr)
explicit binary_evaluator(const XprType& xpr)
: m_functor(xpr.functor()),
m_lhsImpl(xpr.lhs()),
m_rhsImpl(xpr.rhs())

View File

@@ -29,7 +29,7 @@ struct unary_evaluator<CwiseUnaryOp<UnaryOp,ArgType>, IteratorBased>
Flags = XprType::Flags
};
unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression()) {}
explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression()) {}
protected:
typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
@@ -104,7 +104,7 @@ struct unary_evaluator<CwiseUnaryView<ViewOp,ArgType>, IteratorBased>
Flags = XprType::Flags
};
unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression()) {}
explicit unary_evaluator(const XprType& op) : m_functor(op.functor()), m_argImpl(op.nestedExpression()) {}
protected:
typedef typename evaluator<ArgType>::InnerIterator EvalIterator;

View File

@@ -173,7 +173,8 @@ protected:
typedef Product<LhsT,RhsT,DefaultProduct> ProdXprType;
// if the actual left-hand side is a dense vector,
// then build a sparse-view so that we can seamlessly iterator over it.
// then build a sparse-view so that we can seamlessly iterate over it.
// FIXME ActualLhs does not seem to be necessary
typedef typename conditional<is_same<typename internal::traits<Lhs1>::StorageKind,Sparse>::value,
Lhs1, SparseView<Lhs1> >::type ActualLhs;
typedef typename conditional<is_same<typename internal::traits<Lhs1>::StorageKind,Sparse>::value,
@@ -228,7 +229,7 @@ public:
Scalar m_factor;
};
sparse_dense_outer_product_evaluator(const ActualLhs &lhs, const ActualRhs &rhs)
sparse_dense_outer_product_evaluator(const Lhs1 &lhs, const ActualRhs &rhs)
: m_lhs(lhs), m_lhsXprImpl(m_lhs), m_rhsXprImpl(rhs)
{}
@@ -253,7 +254,7 @@ struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, OuterProduct, Sparse
typedef Product<Lhs, Rhs> XprType;
typedef typename XprType::PlainObject PlainObject;
product_evaluator(const XprType& xpr)
explicit product_evaluator(const XprType& xpr)
: Base(xpr.lhs(), xpr.rhs())
{}
@@ -268,7 +269,7 @@ struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, OuterProduct, DenseS
typedef Product<Lhs, Rhs> XprType;
typedef typename XprType::PlainObject PlainObject;
product_evaluator(const XprType& xpr)
explicit product_evaluator(const XprType& xpr)
: Base(xpr.lhs(), xpr.rhs())
{}

View File

@@ -44,7 +44,7 @@ struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, Diagonal
enum { CoeffReadCost = Dynamic, Flags = Rhs::Flags&RowMajorBit }; // FIXME CoeffReadCost & Flags
typedef sparse_diagonal_product_evaluator<Rhs, typename Lhs::DiagonalVectorType, Rhs::Flags&RowMajorBit?SDP_AsScalarProduct:SDP_AsCwiseProduct> Base;
product_evaluator(const XprType& xpr) : Base(xpr.rhs(), xpr.lhs().diagonal()) {}
explicit product_evaluator(const XprType& xpr) : Base(xpr.rhs(), xpr.lhs().diagonal()) {}
};
template<typename Lhs, typename Rhs, int ProductTag>
@@ -57,7 +57,7 @@ struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, SparseSh
enum { CoeffReadCost = Dynamic, Flags = Lhs::Flags&RowMajorBit }; // FIXME CoeffReadCost & Flags
typedef sparse_diagonal_product_evaluator<Lhs, Transpose<const typename Rhs::DiagonalVectorType>, Lhs::Flags&RowMajorBit?SDP_AsCwiseProduct:SDP_AsScalarProduct> Base;
product_evaluator(const XprType& xpr) : Base(xpr.lhs(), xpr.rhs().diagonal()) {}
explicit product_evaluator(const XprType& xpr) : Base(xpr.lhs(), xpr.rhs().diagonal().transpose()) {}
};
template<typename SparseXprType, typename DiagonalCoeffType>

View File

@@ -89,6 +89,8 @@ class SparseMatrix
EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(SparseMatrix, -=)
typedef MappedSparseMatrix<Scalar,Flags> Map;
typedef Diagonal<const SparseMatrix> DiagonalReturnType;
using Base::IsRowMajor;
typedef internal::CompressedStorage<Scalar,Index> Storage;
enum {
@@ -621,7 +623,7 @@ class SparseMatrix
}
/** \returns a const expression of the diagonal coefficients */
const Diagonal<const SparseMatrix> diagonal() const { return *this; }
const DiagonalReturnType diagonal() const { return DiagonalReturnType(*this); }
/** Default constructor yielding an empty \c 0 \c x \c 0 matrix */
inline SparseMatrix()
@@ -1272,7 +1274,7 @@ struct evaluator<SparseMatrix<_Scalar,_Options,_Index> >
};
evaluator() : m_matrix(0) {}
evaluator(const SparseMatrixType &mat) : m_matrix(&mat) {}
explicit evaluator(const SparseMatrixType &mat) : m_matrix(&mat) {}
operator SparseMatrixType&() { return m_matrix->const_cast_derived(); }
operator const SparseMatrixType&() const { return *m_matrix; }

View File

@@ -94,6 +94,9 @@ template<typename Derived> class SparseMatrixBase : public EigenBase<Derived>
CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, Eigen::Transpose<const Derived> >,
Transpose<const Derived>
>::type AdjointReturnType;
typedef Transpose<Derived> TransposeReturnType;
template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SelfAdjointView<Derived, UpLo> Type; };
typedef typename internal::add_const<Transpose<const Derived> >::type ConstTransposeReturnType;
// FIXME storage order do not match evaluator storage order
typedef SparseMatrix<Scalar, Flags&RowMajorBit ? RowMajor : ColMajor, Index> PlainObject;
@@ -114,6 +117,8 @@ template<typename Derived> class SparseMatrixBase : public EigenBase<Derived>
/** \internal Represents a matrix with all coefficients equal to one another*/
typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,Matrix<Scalar,Dynamic,Dynamic> > ConstantReturnType;
/** type of the equivalent dense matrix */
typedef Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> DenseMatrixType;
/** type of the equivalent square matrix */
typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime)> SquareMatrixType;
@@ -303,9 +308,9 @@ template<typename Derived> class SparseMatrixBase : public EigenBase<Derived>
RealScalar norm() const;
RealScalar blueNorm() const;
Transpose<Derived> transpose() { return derived(); }
const Transpose<const Derived> transpose() const { return derived(); }
const AdjointReturnType adjoint() const { return transpose(); }
TransposeReturnType transpose() { return TransposeReturnType(derived()); }
const ConstTransposeReturnType transpose() const { return ConstTransposeReturnType(derived()); }
const AdjointReturnType adjoint() const { return AdjointReturnType(transpose()); }
// inner-vector
typedef Block<Derived,IsRowMajor?1:Dynamic,IsRowMajor?Dynamic:1,true> InnerVectorReturnType;
@@ -317,9 +322,9 @@ template<typename Derived> class SparseMatrixBase : public EigenBase<Derived>
Block<Derived,Dynamic,Dynamic,true> innerVectors(Index outerStart, Index outerSize);
const Block<const Derived,Dynamic,Dynamic,true> innerVectors(Index outerStart, Index outerSize) const;
Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> toDense() const
DenseMatrixType toDense() const
{
return derived();
return DenseMatrixType(derived());
}
template<typename OtherDerived>

View File

@@ -166,7 +166,7 @@ struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, Permutat
typedef typename traits<permut_sparsematrix_product_retval<Lhs,Rhs,OnTheRight,false> >::ReturnType PlainObject;
typedef typename evaluator<PlainObject>::type Base;
product_evaluator(const XprType& xpr)
explicit product_evaluator(const XprType& xpr)
: m_result(xpr.rows(), xpr.cols())
{
::new (static_cast<Base*>(this)) Base(m_result);
@@ -185,7 +185,7 @@ struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, SparseSh
typedef typename traits<permut_sparsematrix_product_retval<Rhs,Lhs,OnTheRight,false> >::ReturnType PlainObject;
typedef typename evaluator<PlainObject>::type Base;
product_evaluator(const XprType& xpr)
explicit product_evaluator(const XprType& xpr)
: m_result(xpr.rows(), xpr.cols())
{
::new (static_cast<Base*>(this)) Base(m_result);

View File

@@ -59,7 +59,7 @@ struct evaluator<SparseView<Product<Lhs, Rhs, Options> > >
typedef evaluator type;
typedef evaluator nestedType;
evaluator(const XprType& xpr)
explicit evaluator(const XprType& xpr)
: m_result(xpr.rows(), xpr.cols())
{
using std::abs;

View File

@@ -53,7 +53,7 @@ template<typename MatrixType, unsigned int _Mode> class SparseSelfAdjointView
typedef typename MatrixType::Nested MatrixTypeNested;
typedef typename internal::remove_all<MatrixTypeNested>::type _MatrixTypeNested;
inline SparseSelfAdjointView(const MatrixType& matrix) : m_matrix(matrix)
explicit inline SparseSelfAdjointView(const MatrixType& matrix) : m_matrix(matrix)
{
eigen_assert(rows()==cols() && "SelfAdjointView is only for squared matrices");
}
@@ -172,14 +172,14 @@ template<typename Derived>
template<unsigned int Mode>
const SparseSelfAdjointView<Derived, Mode> SparseMatrixBase<Derived>::selfadjointView() const
{
return derived();
return SparseSelfAdjointView<Derived, Mode>(derived());
}
template<typename Derived>
template<unsigned int Mode>
SparseSelfAdjointView<Derived, Mode> SparseMatrixBase<Derived>::selfadjointView()
{
return derived();
return SparseSelfAdjointView<Derived, Mode>(derived());
}
/***************************************************************************

View File

@@ -62,7 +62,7 @@ struct unary_evaluator<Transpose<ArgType>, IteratorBased>
Flags = XprType::Flags
};
unary_evaluator(const XprType& op) :m_argImpl(op.nestedExpression()) {}
explicit unary_evaluator(const XprType& op) :m_argImpl(op.nestedExpression()) {}
protected:
typename evaluator<ArgType>::nestedType m_argImpl;

View File

@@ -185,7 +185,7 @@ public:
Flags = XprType::Flags
};
unary_evaluator(const XprType &xpr) : m_argImpl(xpr.nestedExpression()) {}
explicit unary_evaluator(const XprType &xpr) : m_argImpl(xpr.nestedExpression()) {}
class InnerIterator : public EvalIterator
{
@@ -269,7 +269,7 @@ template<int Mode>
inline const TriangularView<Derived, Mode>
SparseMatrixBase<Derived>::triangularView() const
{
return derived();
return TriangularView<Derived, Mode>(derived());
}
} // end namespace Eigen

View File

@@ -221,7 +221,7 @@ class SparseVector
inline SparseVector() : m_size(0) { check_template_parameters(); resize(0); }
inline SparseVector(Index size) : m_size(0) { check_template_parameters(); resize(size); }
explicit inline SparseVector(Index size) : m_size(0) { check_template_parameters(); resize(size); }
inline SparseVector(Index rows, Index cols) : m_size(0) { check_template_parameters(); resize(rows,cols); }
@@ -360,14 +360,14 @@ template<typename Scalar, int _Options, typename _Index>
class SparseVector<Scalar,_Options,_Index>::InnerIterator
{
public:
InnerIterator(const SparseVector& vec, Index outer=0)
explicit InnerIterator(const SparseVector& vec, Index outer=0)
: m_data(vec.m_data), m_id(0), m_end(static_cast<Index>(m_data.size()))
{
EIGEN_UNUSED_VARIABLE(outer);
eigen_assert(outer==0);
}
InnerIterator(const internal::CompressedStorage<Scalar,Index>& data)
explicit InnerIterator(const internal::CompressedStorage<Scalar,Index>& data)
: m_data(data), m_id(0), m_end(static_cast<Index>(m_data.size()))
{}
@@ -392,14 +392,14 @@ template<typename Scalar, int _Options, typename _Index>
class SparseVector<Scalar,_Options,_Index>::ReverseInnerIterator
{
public:
ReverseInnerIterator(const SparseVector& vec, Index outer=0)
explicit ReverseInnerIterator(const SparseVector& vec, Index outer=0)
: m_data(vec.m_data), m_id(static_cast<Index>(m_data.size())), m_start(0)
{
EIGEN_UNUSED_VARIABLE(outer);
eigen_assert(outer==0);
}
ReverseInnerIterator(const internal::CompressedStorage<Scalar,Index>& data)
explicit ReverseInnerIterator(const internal::CompressedStorage<Scalar,Index>& data)
: m_data(data), m_id(static_cast<Index>(m_data.size())), m_start(0)
{}
@@ -435,7 +435,7 @@ struct evaluator<SparseVector<_Scalar,_Options,_Index> >
Flags = SparseVectorType::Flags
};
evaluator(const SparseVectorType &mat) : m_matrix(mat) {}
explicit evaluator(const SparseVectorType &mat) : m_matrix(mat) {}
operator SparseVectorType&() { return m_matrix.const_cast_derived(); }
operator const SparseVectorType&() const { return m_matrix; }

View File

@@ -36,7 +36,7 @@ public:
EIGEN_SPARSE_PUBLIC_INTERFACE(SparseView)
typedef typename internal::remove_all<MatrixType>::type NestedExpression;
SparseView(const MatrixType& mat, const Scalar& m_reference = Scalar(0),
explicit SparseView(const MatrixType& mat, const Scalar& m_reference = Scalar(0),
RealScalar m_epsilon = NumTraits<Scalar>::dummy_precision()) :
m_matrix(mat), m_reference(m_reference), m_epsilon(m_epsilon) {}
@@ -111,7 +111,7 @@ struct unary_evaluator<SparseView<ArgType>, IteratorBased>
Flags = XprType::Flags
};
unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
protected:
typename evaluator<ArgType>::nestedType m_argImpl;
@@ -180,7 +180,7 @@ struct unary_evaluator<SparseView<ArgType>, IndexBased>
Flags = XprType::Flags
};
unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
protected:
typename evaluator<ArgType>::nestedType m_argImpl;