From 5da60897abba95e4b0c9928dce0f1f18efab4580 Mon Sep 17 00:00:00 2001
From: Benoit Jacob <jacob.benoit.1@gmail.com>
Date: Wed, 14 May 2008 08:20:15 +0000
Subject: [PATCH] Introduce generic Flagged xpr, remove already Lazy.h and
 Temporary.h Rename DefaultLostFlagMask --> HerediraryBits

---
 Eigen/Core                                |  3 +-
 Eigen/src/Core/Block.h                    |  2 +-
 Eigen/src/Core/CwiseBinaryOp.h            |  2 +-
 Eigen/src/Core/CwiseNullaryOp.h           |  2 +-
 Eigen/src/Core/CwiseUnaryOp.h             |  2 +-
 Eigen/src/Core/DiagonalCoeffs.h           |  2 +-
 Eigen/src/Core/DiagonalMatrix.h           |  2 +-
 Eigen/src/Core/{Temporary.h => Flagged.h} | 55 ++++++++-----
 Eigen/src/Core/Lazy.h                     | 97 -----------------------
 Eigen/src/Core/Map.h                      |  2 +-
 Eigen/src/Core/MatrixBase.h               | 17 +++-
 Eigen/src/Core/Minor.h                    |  2 +-
 Eigen/src/Core/Product.h                  |  2 +-
 Eigen/src/Core/ProductWIP.h               |  2 +-
 Eigen/src/Core/Redux.h                    |  2 +-
 Eigen/src/Core/Transpose.h                |  6 +-
 Eigen/src/Core/Triangular.h               | 16 ++--
 Eigen/src/Core/util/Constants.h           |  9 ++-
 Eigen/src/Core/util/ForwardDeclarations.h |  3 +-
 Eigen/src/Core/util/Macros.h              |  1 +
 Eigen/src/Core/util/Meta.h                |  7 +-
 21 files changed, 80 insertions(+), 156 deletions(-)
 rename Eigen/src/Core/{Temporary.h => Flagged.h} (59%)
 delete mode 100644 Eigen/src/Core/Lazy.h

diff --git a/Eigen/Core b/Eigen/Core
index 82ca1f165..e5310d732 100644
--- a/Eigen/Core
+++ b/Eigen/Core
@@ -30,9 +30,8 @@ namespace Eigen {
 #include "src/Core/Coeffs.h"
 #include "src/Core/Assign.h"
 #include "src/Core/MatrixStorage.h"
+#include "src/Core/Flagged.h"
 #include "src/Core/Matrix.h"
-#include "src/Core/Lazy.h"
-#include "src/Core/Temporary.h"
 #include "src/Core/CwiseBinaryOp.h"
 #include "src/Core/CwiseUnaryOp.h"
 #include "src/Core/CwiseNullaryOp.h"
diff --git a/Eigen/src/Core/Block.h b/Eigen/src/Core/Block.h
index 125ae3979..ef6b84eff 100644
--- a/Eigen/src/Core/Block.h
+++ b/Eigen/src/Core/Block.h
@@ -71,7 +71,7 @@ struct ei_traits<Block<MatrixType, BlockRows, BlockCols> >
                       || (ColsAtCompileTime != Dynamic && MatrixType::ColsAtCompileTime == Dynamic))
                       ? ~LargeBit
                       : ~(unsigned int)0,
-    Flags = MatrixType::Flags & (DefaultLostFlagMask | VectorizableBit | DirectAccessBit) & FlagsMaskLargeBit,
+    Flags = MatrixType::Flags & (HereditaryBits | VectorizableBit | DirectAccessBit) & FlagsMaskLargeBit,
     CoeffReadCost = MatrixType::CoeffReadCost
   };
 };
diff --git a/Eigen/src/Core/CwiseBinaryOp.h b/Eigen/src/Core/CwiseBinaryOp.h
index dde7de437..28986d9a2 100644
--- a/Eigen/src/Core/CwiseBinaryOp.h
+++ b/Eigen/src/Core/CwiseBinaryOp.h
@@ -69,7 +69,7 @@ struct ei_traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
     MaxRowsAtCompileTime = Lhs::MaxRowsAtCompileTime,
     MaxColsAtCompileTime = Lhs::MaxColsAtCompileTime,
     Flags = ((int(LhsFlags) | int(RhsFlags)) & (
-        DefaultLostFlagMask
+        HereditaryBits
       | Like1DArrayBit
       | (ei_functor_traits<BinaryOp>::IsVectorizable && ((int(LhsFlags) & RowMajorBit)==(int(RhsFlags) & RowMajorBit))
         ? int(LhsFlags) & int(RhsFlags) & VectorizableBit : 0))),
diff --git a/Eigen/src/Core/CwiseNullaryOp.h b/Eigen/src/Core/CwiseNullaryOp.h
index 8e28919bb..5efcc27f3 100644
--- a/Eigen/src/Core/CwiseNullaryOp.h
+++ b/Eigen/src/Core/CwiseNullaryOp.h
@@ -49,7 +49,7 @@ struct ei_traits<CwiseNullaryOp<NullaryOp, MatrixType> >
     MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
     MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
     Flags = (MatrixType::Flags
-      & (DefaultLostFlagMask | Like1DArrayBit | (ei_functor_traits<NullaryOp>::IsVectorizable ? VectorizableBit : 0)))
+      & (HereditaryBits | Like1DArrayBit | (ei_functor_traits<NullaryOp>::IsVectorizable ? VectorizableBit : 0)))
       | (ei_functor_traits<NullaryOp>::IsRepeatable ? 0 : EvalBeforeNestingBit),
     CoeffReadCost = ei_functor_traits<NullaryOp>::Cost
   };
diff --git a/Eigen/src/Core/CwiseUnaryOp.h b/Eigen/src/Core/CwiseUnaryOp.h
index d7a415d48..e1527da84 100644
--- a/Eigen/src/Core/CwiseUnaryOp.h
+++ b/Eigen/src/Core/CwiseUnaryOp.h
@@ -55,7 +55,7 @@ struct ei_traits<CwiseUnaryOp<UnaryOp, MatrixType> >
     MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
     MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
     Flags = (MatrixTypeFlags & (
-      DefaultLostFlagMask | Like1DArrayBit
+      HereditaryBits | Like1DArrayBit
       | (ei_functor_traits<UnaryOp>::IsVectorizable ? VectorizableBit : 0))),
     CoeffReadCost = MatrixTypeCoeffReadCost + ei_functor_traits<UnaryOp>::Cost
   };
diff --git a/Eigen/src/Core/DiagonalCoeffs.h b/Eigen/src/Core/DiagonalCoeffs.h
index 38866d95d..75469b4bf 100644
--- a/Eigen/src/Core/DiagonalCoeffs.h
+++ b/Eigen/src/Core/DiagonalCoeffs.h
@@ -56,7 +56,7 @@ struct ei_traits<DiagonalCoeffs<MatrixType> >
     MaxColsAtCompileTime = 1,
     Flags = (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic
             ? (unsigned int)_MatrixTypeNested::Flags
-            : (unsigned int)_MatrixTypeNested::Flags &~ LargeBit) & DefaultLostFlagMask,
+            : (unsigned int)_MatrixTypeNested::Flags &~ LargeBit) & HereditaryBits,
     CoeffReadCost = _MatrixTypeNested::CoeffReadCost
   };
 };
diff --git a/Eigen/src/Core/DiagonalMatrix.h b/Eigen/src/Core/DiagonalMatrix.h
index 030b1b732..bd420511f 100644
--- a/Eigen/src/Core/DiagonalMatrix.h
+++ b/Eigen/src/Core/DiagonalMatrix.h
@@ -49,7 +49,7 @@ struct ei_traits<DiagonalMatrix<CoeffsVectorType> >
     ColsAtCompileTime = CoeffsVectorType::SizeAtCompileTime,
     MaxRowsAtCompileTime = CoeffsVectorType::MaxSizeAtCompileTime,
     MaxColsAtCompileTime = CoeffsVectorType::MaxSizeAtCompileTime,
-    Flags = _CoeffsVectorTypeTypeNested::Flags & DefaultLostFlagMask,
+    Flags = _CoeffsVectorTypeTypeNested::Flags & HereditaryBits,
     CoeffReadCost = _CoeffsVectorTypeTypeNested::CoeffReadCost
   };
 };
diff --git a/Eigen/src/Core/Temporary.h b/Eigen/src/Core/Flagged.h
similarity index 59%
rename from Eigen/src/Core/Temporary.h
rename to Eigen/src/Core/Flagged.h
index ed62ea786..8ec1452e1 100644
--- a/Eigen/src/Core/Temporary.h
+++ b/Eigen/src/Core/Flagged.h
@@ -1,8 +1,7 @@
 // This file is part of Eigen, a lightweight C++ template library
 // for linear algebra. Eigen itself is part of the KDE project.
 //
-// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
-// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
+// Copyright (C) 2008 Benoit Jacob <jacob@math.jussieu.fr>
 //
 // Eigen is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -23,23 +22,25 @@
 // License and a copy of the GNU General Public License along with
 // Eigen. If not, see <http://www.gnu.org/licenses/>.
 
-#ifndef EIGEN_TEMPORARY_H
-#define EIGEN_TEMPORARY_H
+#ifndef EIGEN_FLAGGED_H
+#define EIGEN_FLAGGED_H
 
-/** \class Temporary
+/** \class Flagged
   *
-  * \brief Expression with the temporary flag set
+  * \brief Expression with modified flags
   *
-  * \param ExpressionType the type of the object of which we are taking the temporary version
+  * \param ExpressionType the type of the object of which we are modifying the flags
+  * \param Added the flags added to the expression
+  * \param Removed the flags removed from the expression (has priority over Added).
   *
-  * This class represents the temporary version of an expression.
-  * It is the return type of MatrixBase::temporary()
+  * This class represents an expression whose flags have been modified
+  * It is the return type of MatrixBase::flagged()
   * and most of the time this is the only way it is used.
   *
-  * \sa MatrixBase::temporary()
+  * \sa MatrixBase::flagged()
   */
-template<typename ExpressionType>
-struct ei_traits<Temporary<ExpressionType> >
+template<typename ExpressionType, unsigned int Added, unsigned int Removed>
+struct ei_traits<Flagged<ExpressionType, Added, Removed> >
 {
   typedef typename ExpressionType::Scalar Scalar;
   enum {
@@ -47,19 +48,19 @@ struct ei_traits<Temporary<ExpressionType> >
     ColsAtCompileTime = ExpressionType::ColsAtCompileTime,
     MaxRowsAtCompileTime = ExpressionType::MaxRowsAtCompileTime,
     MaxColsAtCompileTime = ExpressionType::MaxColsAtCompileTime,
-    Flags = ExpressionType::Flags,
+    Flags = (ExpressionType::Flags | Added) & ~Removed,
     CoeffReadCost = ExpressionType::CoeffReadCost
   };
 };
 
-template<typename ExpressionType> class Temporary
-  : public MatrixBase<Temporary<ExpressionType> >
+template<typename ExpressionType, unsigned int Added, unsigned int Removed> class Flagged
+  : public MatrixBase<Flagged<ExpressionType, Added, Removed> >
 {
   public:
 
-    EIGEN_GENERIC_PUBLIC_INTERFACE(Temporary)
+    EIGEN_GENERIC_PUBLIC_INTERFACE(Flagged)
 
-    inline Temporary(const ExpressionType& matrix) : m_expression(matrix) {}
+    inline Flagged(const ExpressionType& matrix) : m_expression(matrix) {}
 
   private:
 
@@ -71,12 +72,23 @@ template<typename ExpressionType> class Temporary
       return m_expression.coeff(row, col);
     }
 
+    inline Scalar& _coeffRef(int row, int col)
+    {
+      return m_expression.const_cast_derived().coeffRef(row, col);
+    }
+
     template<int LoadMode>
     inline const PacketScalar _packetCoeff(int row, int col) const
     {
       return m_expression.template packetCoeff<LoadMode>(row, col);
     }
 
+    template<int LoadMode>
+    inline void _writePacketCoeff(int row, int col, const PacketScalar& x)
+    {
+      m_expression.const_cast_derived().template writePacketCoeff<LoadMode>(row, col, x);
+    }
+
   protected:
     const ExpressionType m_expression;
 };
@@ -84,10 +96,11 @@ template<typename ExpressionType> class Temporary
 /** \returns an expression of the temporary version of *this.
   */
 template<typename Derived>
-inline const Temporary<Derived>
-MatrixBase<Derived>::temporary() const
+template<unsigned int Added, unsigned int Removed>
+inline const Flagged<Derived, Added, Removed>
+MatrixBase<Derived>::flagged() const
 {
-  return Temporary<Derived>(derived());
+  return Flagged<Derived, Added, Removed>(derived());
 }
 
-#endif // EIGEN_TEMPORARY_H
+#endif // EIGEN_FLAGGED_H
diff --git a/Eigen/src/Core/Lazy.h b/Eigen/src/Core/Lazy.h
deleted file mode 100644
index ef02c9832..000000000
--- a/Eigen/src/Core/Lazy.h
+++ /dev/null
@@ -1,97 +0,0 @@
-// This file is part of Eigen, a lightweight C++ template library
-// for linear algebra. Eigen itself is part of the KDE project.
-//
-// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
-//
-// Eigen is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 3 of the License, or (at your option) any later version.
-//
-// Alternatively, you can redistribute it and/or
-// modify it under the terms of the GNU General Public License as
-// published by the Free Software Foundation; either version 2 of
-// the License, or (at your option) any later version.
-//
-// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
-// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License and a copy of the GNU General Public License along with
-// Eigen. If not, see <http://www.gnu.org/licenses/>.
-
-#ifndef EIGEN_LAZY_H
-#define EIGEN_LAZY_H
-
-/** \class Lazy
-  *
-  * \brief Expression with the lazy flag set
-  *
-  * \param ExpressionType the type of the object of which we are taking the lazy version
-  *
-  * This class represents the lazy version of an expression.
-  * It is the return type of MatrixBase::lazy()
-  * and most of the time this is the only way it is used.
-  *
-  * \sa MatrixBase::lazy()
-  */
-template<typename ExpressionType>
-struct ei_traits<Lazy<ExpressionType> >
-{
-  typedef typename ExpressionType::Scalar Scalar;
-  enum {
-    RowsAtCompileTime = ExpressionType::RowsAtCompileTime,
-    ColsAtCompileTime = ExpressionType::ColsAtCompileTime,
-    MaxRowsAtCompileTime = ExpressionType::MaxRowsAtCompileTime,
-    MaxColsAtCompileTime = ExpressionType::MaxColsAtCompileTime,
-    Flags = ExpressionType::Flags & ~(EvalBeforeNestingBit | EvalBeforeAssigningBit),
-    CoeffReadCost = ExpressionType::CoeffReadCost
-  };
-};
-
-template<typename ExpressionType> class Lazy
-  : public MatrixBase<Lazy<ExpressionType> >
-{
-  public:
-
-    EIGEN_GENERIC_PUBLIC_INTERFACE(Lazy)
-
-    inline Lazy(const ExpressionType& matrix) : m_expression(matrix) {}
-
-    EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Lazy)
-
-  private:
-
-    inline int _rows() const { return m_expression.rows(); }
-    inline int _cols() const { return m_expression.cols(); }
-
-    inline const Scalar _coeff(int row, int col) const
-    {
-      return m_expression.coeff(row, col);
-    }
-
-    template<int LoadMode>
-    inline PacketScalar _packetCoeff(int row, int col) const
-    {
-      return m_expression.template packetCoeff<LoadMode>(row, col);
-    }
-
-  protected:
-    const ExpressionType& m_expression;
-};
-
-/** \returns an expression of the lazy version of *this.
-  *
-  * Example: \include MatrixBase_lazy.cpp
-  * Output: \verbinclude MatrixBase_lazy.out
-  */
-template<typename Derived>
-inline const Lazy<Derived>
-MatrixBase<Derived>::lazy() const
-{
-  return Lazy<Derived>(derived());
-}
-
-#endif // EIGEN_LAZY_H
diff --git a/Eigen/src/Core/Map.h b/Eigen/src/Core/Map.h
index a503043ac..1eb879193 100644
--- a/Eigen/src/Core/Map.h
+++ b/Eigen/src/Core/Map.h
@@ -47,7 +47,7 @@ struct ei_traits<Map<MatrixType> >
     ColsAtCompileTime = MatrixType::ColsAtCompileTime,
     MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
     MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
-    Flags = MatrixType::Flags & (DefaultLostFlagMask | DirectAccessBit),
+    Flags = MatrixType::Flags & (HereditaryBits | DirectAccessBit),
     CoeffReadCost = NumTraits<Scalar>::ReadCost
   };
 };
diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h
index d14fec1f1..ee6a197a9 100644
--- a/Eigen/src/Core/MatrixBase.h
+++ b/Eigen/src/Core/MatrixBase.h
@@ -278,7 +278,9 @@ template<typename Derived> class MatrixBase
 
     Transpose<Derived> transpose();
     const Transpose<Derived> transpose() const;
-    const Transpose<Temporary<CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived> > >
+    const Transpose<
+            Flagged<CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived>
+            , TemporaryBit, 0> >
     adjoint() const;
     //@}
 
@@ -419,8 +421,17 @@ template<typename Derived> class MatrixBase
     template<typename OtherDerived>
     void swap(const MatrixBase<OtherDerived>& other);
 
-    const Lazy<Derived> lazy() const;
-    const Temporary<Derived> temporary() const;
+    template<unsigned int Added, unsigned int Removed>
+    const Flagged<Derived, Added, Removed> flagged() const;
+
+    const Flagged<Derived, 0, EvalBeforeNestingBit | EvalBeforeAssigningBit> lazy() const
+    {
+      return derived();
+    }
+    const Flagged<Derived, TemporaryBit, 0> temporary() const
+    {
+      return derived();
+    }
 
     /** \returns number of elements to skip to pass from one row (resp. column) to another
       * for a row-major (resp. column-major) matrix.
diff --git a/Eigen/src/Core/Minor.h b/Eigen/src/Core/Minor.h
index d7e08c4e0..d22f41982 100644
--- a/Eigen/src/Core/Minor.h
+++ b/Eigen/src/Core/Minor.h
@@ -52,7 +52,7 @@ struct ei_traits<Minor<MatrixType> >
                              int(MatrixType::MaxRowsAtCompileTime) - 1 : Dynamic,
     MaxColsAtCompileTime = (MatrixType::MaxColsAtCompileTime != Dynamic) ?
                              int(MatrixType::MaxColsAtCompileTime) - 1 : Dynamic,
-    Flags = _MatrixTypeNested::Flags & DefaultLostFlagMask,
+    Flags = _MatrixTypeNested::Flags & HereditaryBits,
     CoeffReadCost = _MatrixTypeNested::CoeffReadCost
   };
 };
diff --git a/Eigen/src/Core/Product.h b/Eigen/src/Core/Product.h
index 414843651..5d3e99281 100644
--- a/Eigen/src/Core/Product.h
+++ b/Eigen/src/Core/Product.h
@@ -179,7 +179,7 @@ struct ei_traits<Product<Lhs, Rhs, EvalMode> >
     _Vectorizable = (_LhsVectorizable || _RhsVectorizable) ? 1 : 0,
     _RowMajor = (RhsFlags & RowMajorBit)
               && (EvalMode==(int)CacheFriendlyProduct ? (int)LhsFlags & RowMajorBit : (!_LhsVectorizable)),
-    _LostBits = DefaultLostFlagMask & ~(
+    _LostBits = HereditaryBits & ~(
                 (_RowMajor ? 0 : RowMajorBit)
               | ((RowsAtCompileTime == Dynamic || ColsAtCompileTime == Dynamic) ? 0 : LargeBit)),
     Flags = ((unsigned int)(LhsFlags | RhsFlags) & _LostBits)
diff --git a/Eigen/src/Core/ProductWIP.h b/Eigen/src/Core/ProductWIP.h
index f7c8c3dc0..b7865bb70 100644
--- a/Eigen/src/Core/ProductWIP.h
+++ b/Eigen/src/Core/ProductWIP.h
@@ -227,7 +227,7 @@ struct ei_traits<Product<Lhs, Rhs, EvalMode> >
     _Vectorizable = (_LhsVectorizable || _RhsVectorizable) ? 0 : 0,
     _RowMajor = (RhsFlags & RowMajorBit)
               && (EvalMode==(int)CacheFriendlyProduct ? (int)LhsFlags & RowMajorBit : (!_LhsVectorizable)),
-    _LostBits = DefaultLostFlagMask & ~(
+    _LostBits = HereditaryBits & ~(
                 (_RowMajor ? 0 : RowMajorBit)
               | ((RowsAtCompileTime == Dynamic || ColsAtCompileTime == Dynamic) ? 0 : LargeBit)),
     Flags = ((unsigned int)(LhsFlags | RhsFlags) & _LostBits)
diff --git a/Eigen/src/Core/Redux.h b/Eigen/src/Core/Redux.h
index 5672effc7..aaf1843d9 100644
--- a/Eigen/src/Core/Redux.h
+++ b/Eigen/src/Core/Redux.h
@@ -96,7 +96,7 @@ struct ei_traits<PartialRedux<Direction, BinaryOp, MatrixType> >
     MaxColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::MaxColsAtCompileTime,
     Flags = ((int(RowsAtCompileTime) == Dynamic || int(ColsAtCompileTime) == Dynamic)
           ? (unsigned int)_MatrixTypeNested::Flags
-          : (unsigned int)_MatrixTypeNested::Flags & ~LargeBit) & DefaultLostFlagMask,
+          : (unsigned int)_MatrixTypeNested::Flags & ~LargeBit) & HereditaryBits,
     TraversalSize = Direction==Vertical ? RowsAtCompileTime : ColsAtCompileTime,
     CoeffReadCost = TraversalSize * _MatrixTypeNested::CoeffReadCost
                   + (TraversalSize - 1) * ei_functor_traits<BinaryOp>::Cost
diff --git a/Eigen/src/Core/Transpose.h b/Eigen/src/Core/Transpose.h
index 5d4cbd2f4..833dbe287 100644
--- a/Eigen/src/Core/Transpose.h
+++ b/Eigen/src/Core/Transpose.h
@@ -125,9 +125,9 @@ MatrixBase<Derived>::transpose() const
   *
   * \sa transpose(), conjugate(), class Transpose, class ei_scalar_conjugate_op */
 template<typename Derived>
-inline const Transpose<Temporary<
-                     CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived >
-                   > >
+inline const Transpose<
+               Flagged<CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived >
+               , TemporaryBit, 0> >
 MatrixBase<Derived>::adjoint() const
 {
   return conjugate().temporary().transpose();
diff --git a/Eigen/src/Core/Triangular.h b/Eigen/src/Core/Triangular.h
index ff2dcbb37..12f34cd99 100755
--- a/Eigen/src/Core/Triangular.h
+++ b/Eigen/src/Core/Triangular.h
@@ -27,7 +27,7 @@
 
 /** \class Triangular
   *
-  * \brief Expression of a triangular matrix from a squared matrix
+  * \brief Expression of a triangular matrix from a square matrix
   *
   * \param Mode or-ed bit field indicating the triangular part (Upper or Lower) we are taking,
   * and the property of the diagonal if any (UnitDiagBit or NullDiagBit).
@@ -52,7 +52,7 @@
   * lower part of m1 is left unchanged, and optimal loops are employed. Note that
   * m1 might also be resized.
   *
-  * Of course, in both examples \c <any \c expression> has to be a squared matrix.
+  * Of course, in both examples \c <any \c expression> has to be a square matrix.
   *
   * \sa MatrixBase::upper(), MatrixBase::lower(), class TriangularProduct
   */
@@ -89,21 +89,21 @@ template<int Mode, typename MatrixType> class Triangular
     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Triangular)
 
     /** Overloaded to keep a Triangular expression */
-    inline Triangular<(Upper | Lower) ^ Mode, Temporary<Transpose<MatrixType> > > transpose()
+    inline Triangular<(Upper | Lower) ^ Mode, Flagged<Transpose<MatrixType>,TemporaryBit,0> > transpose()
     {
-      return Triangular<(Upper | Lower) ^ Mode, Temporary<Transpose<MatrixType> > >((m_matrix.transpose().temporary()));
+      return m_matrix.transpose().temporary();
     }
 
     /** Overloaded to keep a Triangular expression */
-    inline const Triangular<(Upper | Lower) ^ Mode, Temporary<Transpose<MatrixType> > > transpose() const
+    inline const Triangular<(Upper | Lower) ^ Mode, Flagged<Transpose<MatrixType>,TemporaryBit,0> > transpose() const
     {
-      return Triangular<(Upper | Lower) ^ Mode, Temporary<Transpose<MatrixType> > >((m_matrix.transpose().temporary()));
+      return m_matrix.transpose().temporary();
     }
 
     /** \returns the product of the inverse of *this with \a other.
       *
-      * This function computes the inverse-matrix matrix product inv(*this) \a other
-      * This process is also as forward (resp. backward) substitution if *this is an upper (resp. lower)
+      * This function computes the inverse-matrix matrix product inverse(*this) * \a other
+      * It works as a forward (resp. backward) substitution if *this is an upper (resp. lower)
       * triangular matrix.
       */
     template<typename OtherDerived>
diff --git a/Eigen/src/Core/util/Constants.h b/Eigen/src/Core/util/Constants.h
index 8aa16aa81..12efa9530 100644
--- a/Eigen/src/Core/util/Constants.h
+++ b/Eigen/src/Core/util/Constants.h
@@ -44,13 +44,16 @@ const unsigned int UnitDiagBit = 0x80;      ///< means all diagonal coefficients
 const unsigned int NullLowerBit = 0x200;    ///< means the strictly triangular lower part is 0
 const unsigned int NullUpperBit = 0x400;    ///< means the strictly triangular upper part is 0
 const unsigned int DirectAccessBit = 0x800; ///< means the underlying matrix data can be direclty accessed
+const unsigned int TemporaryBit = 0x1000;   ///< means the expression should be copied by value when nested
 
 enum { Upper=NullLowerBit, Lower=NullUpperBit };
 enum { Aligned=0, UnAligned=1 };
 
-// list of flags that are lost by default
-const unsigned int DefaultLostFlagMask = ~(VectorizableBit | Like1DArrayBit | DirectAccessBit
-  | NullDiagBit | UnitDiagBit | NullLowerBit | NullUpperBit);
+// list of flags that are inherited by default
+const unsigned int HereditaryBits = RowMajorBit
+                                  | EvalBeforeNestingBit
+                                  | EvalBeforeAssigningBit
+                                  | LargeBit;
 
 enum { ConditionalJumpCost = 5 };
 enum CornerType { TopLeft, TopRight, BottomLeft, BottomRight };
diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h
index c18977820..85892eb3a 100644
--- a/Eigen/src/Core/util/ForwardDeclarations.h
+++ b/Eigen/src/Core/util/ForwardDeclarations.h
@@ -34,8 +34,7 @@ template<typename _Scalar, int _Rows, int _Cols,
          int _MaxRows = _Rows, int _MaxCols = _Cols>
 class Matrix;
 
-template<typename ExpressionType> class Lazy;
-template<typename ExpressionType> class Temporary;
+template<typename ExpressionType, unsigned int Added, unsigned int Removed> class Flagged;
 template<typename MatrixType> class Minor;
 template<typename MatrixType, int BlockRows=Dynamic, int BlockCols=Dynamic> class Block;
 template<typename MatrixType> class Transpose;
diff --git a/Eigen/src/Core/util/Macros.h b/Eigen/src/Core/util/Macros.h
index 306144218..a05067c24 100644
--- a/Eigen/src/Core/util/Macros.h
+++ b/Eigen/src/Core/util/Macros.h
@@ -135,6 +135,7 @@ typedef typename Eigen::NumTraits<Scalar>::Real RealScalar; \
 typedef typename Base::PacketScalar PacketScalar; \
 typedef typename Eigen::ei_nested<Derived>::type Nested; \
 typedef typename Eigen::ei_eval<Derived>::type Eval; \
+typedef Eigen::Flagged<Derived, TemporaryBit, 0> Temporary; \
 enum { RowsAtCompileTime = Base::RowsAtCompileTime, \
        ColsAtCompileTime = Base::ColsAtCompileTime, \
        MaxRowsAtCompileTime = Base::MaxRowsAtCompileTime, \
diff --git a/Eigen/src/Core/util/Meta.h b/Eigen/src/Core/util/Meta.h
index 3c4596d4c..9c3630d02 100644
--- a/Eigen/src/Core/util/Meta.h
+++ b/Eigen/src/Core/util/Meta.h
@@ -194,12 +194,7 @@ template<typename T> struct ei_unconst<const T> { typedef T type; };
 
 template<typename T> struct ei_is_temporary
 {
-  enum { ret = 0 };
-};
-
-template<typename T> struct ei_is_temporary<Temporary<T> >
-{
-  enum { ret = 1 };
+  enum { ret = ei_traits<T>::Flags & TemporaryBit };
 };
 
 template<typename T, int n=1> struct ei_nested