From d6f0073913be8bda7e5ea2eb5ec78ed2bf37fbc7 Mon Sep 17 00:00:00 2001
From: Oliver Sander <sander@igpm.rwth-aachen.de>
Date: Mon, 14 Apr 2014 08:27:01 +0200
Subject: [PATCH] Change conversion operators (operator K()) to return
 references instead of values

We simply return a reference to the internal memory instead of copying the object.
This should save some copying when using FieldVector/FieldMatrix with non-trivial
types.  Also, it fixes crashes I have experienced with the ADOL-C automatic
differentiation toolbox [0].

I hope that this patch doesn't have any nasty side effects.  I didn't see any
so far.  Comments for this patch had been requested in FS 1449, and there have
been no objections.

[0] I have contacted the ADOL-C maintainer about this and he said the crashes
are not really bugs in ADOL-C, but that it is simply very difficult to write
a good operator-overloading AD program that can handle operator K returning
values.
---
 dune/common/fmatrix.hh | 2 +-
 dune/common/fvector.hh | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/dune/common/fmatrix.hh b/dune/common/fmatrix.hh
index 7593d52bb..43d715ed2 100644
--- a/dune/common/fmatrix.hh
+++ b/dune/common/fmatrix.hh
@@ -315,7 +315,7 @@ namespace Dune
 
     //===== conversion operator
 
-    operator K () const { return _data[0]; }
+    operator const K& () const { return _data[0]; }
 
   };
 
diff --git a/dune/common/fvector.hh b/dune/common/fvector.hh
index 9e0098d99..bc212c1e5 100644
--- a/dune/common/fvector.hh
+++ b/dune/common/fvector.hh
@@ -269,10 +269,10 @@ namespace Dune {
     //===== conversion operator
 
     /** \brief Conversion operator */
-    operator K () { return _data; }
+    operator K& () { return _data; }
 
     /** \brief Const conversion operator */
-    operator K () const { return _data; }
+    operator const K& () const { return _data; }
   };
 
   /* ----- FV / FV ----- */
-- 
GitLab