use a more c++-ish way of preventing the compiler from generating default operator=

when it's not wanted. Thanks to Christian Mayer for the tip.
This commit is contained in:
Benoit Jacob 2007-12-12 16:52:17 +00:00
parent fa8009c6b7
commit 7ddc13b9fa
12 changed files with 28 additions and 55 deletions

View File

@ -26,8 +26,8 @@
#ifndef EIGEN_CAST_H #ifndef EIGEN_CAST_H
#define EIGEN_CAST_H #define EIGEN_CAST_H
template<typename NewScalar, typename MatrixType> class Cast template<typename NewScalar, typename MatrixType> class Cast : NoDefaultOperatorEquals,
: public MatrixBase<NewScalar, Cast<NewScalar, MatrixType> > public MatrixBase<NewScalar, Cast<NewScalar, MatrixType> >
{ {
public: public:
typedef NewScalar Scalar; typedef NewScalar Scalar;
@ -42,9 +42,6 @@ template<typename NewScalar, typename MatrixType> class Cast
Cast(const Cast& other) Cast(const Cast& other)
: m_matrix(other.m_matrix) {} : m_matrix(other.m_matrix) {}
// assignments are illegal but we still want to intercept them and get clean compile errors
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Cast)
private: private:
const Cast& _ref() const { return *this; } const Cast& _ref() const { return *this; }
int _rows() const { return m_matrix.rows(); } int _rows() const { return m_matrix.rows(); }

View File

@ -26,8 +26,8 @@
#ifndef EIGEN_CONJUGATE_H #ifndef EIGEN_CONJUGATE_H
#define EIGEN_CONJUGATE_H #define EIGEN_CONJUGATE_H
template<typename MatrixType> class Conjugate template<typename MatrixType> class Conjugate : NoDefaultOperatorEquals,
: public MatrixBase<typename MatrixType::Scalar, Conjugate<MatrixType> > public MatrixBase<typename MatrixType::Scalar, Conjugate<MatrixType> >
{ {
public: public:
typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Scalar Scalar;
@ -42,9 +42,6 @@ template<typename MatrixType> class Conjugate
Conjugate(const Conjugate& other) Conjugate(const Conjugate& other)
: m_matrix(other.m_matrix) {} : m_matrix(other.m_matrix) {}
// assignments are illegal but we still want to intercept them and get clean compile errors
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Conjugate)
private: private:
const Conjugate& _ref() const { return *this; } const Conjugate& _ref() const { return *this; }
int _rows() const { return m_matrix.rows(); } int _rows() const { return m_matrix.rows(); }

View File

@ -26,8 +26,8 @@
#ifndef EIGEN_DIFFERENCE_H #ifndef EIGEN_DIFFERENCE_H
#define EIGEN_DIFFERENCE_H #define EIGEN_DIFFERENCE_H
template<typename Lhs, typename Rhs> class Difference template<typename Lhs, typename Rhs> class Difference : NoDefaultOperatorEquals,
: public MatrixBase<typename Lhs::Scalar, Difference<Lhs, Rhs> > public MatrixBase<typename Lhs::Scalar, Difference<Lhs, Rhs> >
{ {
public: public:
typedef typename Lhs::Scalar Scalar; typedef typename Lhs::Scalar Scalar;
@ -47,9 +47,6 @@ template<typename Lhs, typename Rhs> class Difference
Difference(const Difference& other) Difference(const Difference& other)
: m_lhs(other.m_lhs), m_rhs(other.m_rhs) {} : m_lhs(other.m_lhs), m_rhs(other.m_rhs) {}
// assignments are illegal but we still want to intercept them and get clean compile errors
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Difference)
private: private:
const Difference& _ref() const { return *this; } const Difference& _ref() const { return *this; }
int _rows() const { return m_lhs.rows(); } int _rows() const { return m_lhs.rows(); }

View File

@ -26,8 +26,8 @@
#ifndef EIGEN_IDENTITY_H #ifndef EIGEN_IDENTITY_H
#define EIGEN_IDENTITY_H #define EIGEN_IDENTITY_H
template<typename MatrixType> class Identity template<typename MatrixType> class Identity : NoDefaultOperatorEquals,
: public MatrixBase<typename MatrixType::Scalar, Identity<MatrixType> > public MatrixBase<typename MatrixType::Scalar, Identity<MatrixType> >
{ {
public: public:
typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Scalar Scalar;
@ -42,9 +42,6 @@ template<typename MatrixType> class Identity
assert(RowsAtCompileTime == ColsAtCompileTime); assert(RowsAtCompileTime == ColsAtCompileTime);
} }
// assignments are illegal but we still want to intercept them and get clean compile errors
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Identity)
private: private:
Identity& _ref() { return *this; } Identity& _ref() { return *this; }
const Identity& _ref() const { return *this; } const Identity& _ref() const { return *this; }

View File

@ -81,9 +81,6 @@ template<> inline int random(int a, int b)
} }
template<> inline int random() template<> inline int random()
{ {
// "rand() % n" is bad, they say, because the low-order bits are not random enough.
// However here, 21 is odd, so random() % 21 uses the high-order bits
// as well, so there's no problem.
return random<int>(-10, 10); return random<int>(-10, 10);
} }
inline bool isMuchSmallerThan(int a, int, int = precision<int>()) inline bool isMuchSmallerThan(int a, int, int = precision<int>())

View File

@ -26,8 +26,8 @@
#ifndef EIGEN_OPPOSITE_H #ifndef EIGEN_OPPOSITE_H
#define EIGEN_OPPOSITE_H #define EIGEN_OPPOSITE_H
template<typename MatrixType> class Opposite template<typename MatrixType> class Opposite : NoDefaultOperatorEquals,
: public MatrixBase<typename MatrixType::Scalar, Opposite<MatrixType> > public MatrixBase<typename MatrixType::Scalar, Opposite<MatrixType> >
{ {
public: public:
typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Scalar Scalar;
@ -42,9 +42,6 @@ template<typename MatrixType> class Opposite
Opposite(const Opposite& other) Opposite(const Opposite& other)
: m_matrix(other.m_matrix) {} : m_matrix(other.m_matrix) {}
// assignments are illegal but we still want to intercept them and get clean compile errors
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Opposite)
private: private:
const Opposite& _ref() const { return *this; } const Opposite& _ref() const { return *this; }
int _rows() const { return m_matrix.rows(); } int _rows() const { return m_matrix.rows(); }

View File

@ -60,8 +60,8 @@ struct ProductUnroller<Index, 0, Lhs, Rhs>
static void run(int, int, const Lhs&, const Rhs&, typename Lhs::Scalar&) {} static void run(int, int, const Lhs&, const Rhs&, typename Lhs::Scalar&) {}
}; };
template<typename Lhs, typename Rhs> class Product template<typename Lhs, typename Rhs> class Product : NoDefaultOperatorEquals,
: public MatrixBase<typename Lhs::Scalar, Product<Lhs, Rhs> > public MatrixBase<typename Lhs::Scalar, Product<Lhs, Rhs> >
{ {
public: public:
typedef typename Lhs::Scalar Scalar; typedef typename Lhs::Scalar Scalar;
@ -81,9 +81,6 @@ template<typename Lhs, typename Rhs> class Product
Product(const Product& other) Product(const Product& other)
: m_lhs(other.m_lhs), m_rhs(other.m_rhs) {} : m_lhs(other.m_lhs), m_rhs(other.m_rhs) {}
// assignments are illegal but we still want to intercept them and get clean compile errors
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Product)
private: private:
const Product& _ref() const { return *this; } const Product& _ref() const { return *this; }
int _rows() const { return m_lhs.rows(); } int _rows() const { return m_lhs.rows(); }

View File

@ -26,8 +26,8 @@
#ifndef EIGEN_RANDOM_H #ifndef EIGEN_RANDOM_H
#define EIGEN_RANDOM_H #define EIGEN_RANDOM_H
template<typename MatrixType> class Random template<typename MatrixType> class Random : NoDefaultOperatorEquals,
: public MatrixBase<typename MatrixType::Scalar, Random<MatrixType> > public MatrixBase<typename MatrixType::Scalar, Random<MatrixType> >
{ {
public: public:
typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Scalar Scalar;
@ -41,9 +41,6 @@ template<typename MatrixType> class Random
assert(rows > 0 && cols > 0); assert(rows > 0 && cols > 0);
} }
// assignments are illegal but we still want to intercept them and get clean compile errors
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Random)
private: private:
const Random& _ref() const { return *this; } const Random& _ref() const { return *this; }
int _rows() const { return m_rows; } int _rows() const { return m_rows; }

View File

@ -26,8 +26,8 @@
#ifndef EIGEN_SCALARMULTIPLE_H #ifndef EIGEN_SCALARMULTIPLE_H
#define EIGEN_SCALARMULTIPLE_H #define EIGEN_SCALARMULTIPLE_H
template<typename MatrixType> class ScalarMultiple template<typename MatrixType> class ScalarMultiple : NoDefaultOperatorEquals,
: public MatrixBase<typename MatrixType::Scalar, ScalarMultiple<MatrixType> > public MatrixBase<typename MatrixType::Scalar, ScalarMultiple<MatrixType> >
{ {
public: public:
typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Scalar Scalar;
@ -43,9 +43,6 @@ template<typename MatrixType> class ScalarMultiple
ScalarMultiple(const ScalarMultiple& other) ScalarMultiple(const ScalarMultiple& other)
: m_matrix(other.m_matrix), m_scalar(other.m_scalar) {} : m_matrix(other.m_matrix), m_scalar(other.m_scalar) {}
// assignments are illegal but we still want to intercept them and get clean compile errors
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ScalarMultiple)
private: private:
const ScalarMultiple& _ref() const { return *this; } const ScalarMultiple& _ref() const { return *this; }
int _rows() const { return m_matrix.rows(); } int _rows() const { return m_matrix.rows(); }

View File

@ -26,8 +26,8 @@
#ifndef EIGEN_SUM_H #ifndef EIGEN_SUM_H
#define EIGEN_SUM_H #define EIGEN_SUM_H
template<typename Lhs, typename Rhs> class Sum template<typename Lhs, typename Rhs> class Sum : NoDefaultOperatorEquals,
: public MatrixBase<typename Lhs::Scalar, Sum<Lhs, Rhs> > public MatrixBase<typename Lhs::Scalar, Sum<Lhs, Rhs> >
{ {
public: public:
typedef typename Lhs::Scalar Scalar; typedef typename Lhs::Scalar Scalar;
@ -44,11 +44,7 @@ template<typename Lhs, typename Rhs> class Sum
assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols()); assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
} }
Sum(const Sum& other) Sum(const Sum& other) : m_lhs(other.m_lhs), m_rhs(other.m_rhs) {}
: m_lhs(other.m_lhs), m_rhs(other.m_rhs) {}
// assignments are illegal but we still want to intercept them and get clean compile errors
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Sum)
private: private:
const Sum& _ref() const { return *this; } const Sum& _ref() const { return *this; }

View File

@ -124,4 +124,11 @@ enum AssertLevel
InternalDebugging = 2 InternalDebugging = 2
}; };
//classes inheriting NoDefaultOperatorEquals don't generate a default operator=.
class NoDefaultOperatorEquals
{
private:
NoDefaultOperatorEquals& operator=(const NoDefaultOperatorEquals&);
};
#endif // EIGEN_UTIL_H #endif // EIGEN_UTIL_H

View File

@ -26,8 +26,8 @@
#ifndef EIGEN_ZERO_H #ifndef EIGEN_ZERO_H
#define EIGEN_ZERO_H #define EIGEN_ZERO_H
template<typename MatrixType> class Zero template<typename MatrixType> class Zero : NoDefaultOperatorEquals,
: public MatrixBase<typename MatrixType::Scalar, Zero<MatrixType> > public MatrixBase<typename MatrixType::Scalar, Zero<MatrixType> >
{ {
public: public:
typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Scalar Scalar;
@ -41,9 +41,6 @@ template<typename MatrixType> class Zero
assert(rows > 0 && cols > 0); assert(rows > 0 && cols > 0);
} }
// assignments are illegal but we still want to intercept them and get clean compile errors
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Zero)
private: private:
const Zero& _ref() const { return *this; } const Zero& _ref() const { return *this; }
int _rows() const { return m_rows; } int _rows() const { return m_rows; }