================= Const-Correctness [TOC] ================= In FLENS _matrix/vector views_ act and behave like references in C++ just for matrix/vector parts instead of scalar values: +--------------------+---------------------------------------------+ | Pure C++ Types | Analogous Role of FLENS Types | +--------------------+---------------------------------------------+ | int | DenseVector > | +--------------------+---------------------------------------------+ | int & | DenseVector > | +--------------------+---------------------------------------------+ | const int & | const DenseVector > | +--------------------+---------------------------------------------+ Views in FLENS ============== The beauty of this FLENS approach is that you can write functions that take any kind of `DenseVector`: *--[CODE]-----------------------------------------------------------* | | | template | | void | | dummy(const DenseVector &x); | | | *-------------------------------------------------------------------* or *--[CODE]-----------------------------------------------------------* | | | template | | void | | dummy(DenseVector &x); | | | *-------------------------------------------------------------------* Using traits you also can implement functions that accept non-const rvalues *--[CODE]-----------------------------------------------------------* | | | template | | typename RestrictTo::Type, | | void>::Type | | dummy(VX &&x); a | | | *-------------------------------------------------------------------* the latter is necessary in an expression like *--[CODE]-----------------------------------------------------------* | | | dummy(x(_(3,7)); | | | *-------------------------------------------------------------------* where the view is a temporary object. Design Flaw in MTL4 =================== If you want to provide views you really need different types for const and non-const views. Till the moment of this writing (2012/09/01) the __MTL4__ guys think there was a 'simpler way'. As a consequence the _const_ attribute is only decoration. In a large application this can lead to bugs that are hard to find. But even worse, even the compiler can not trace that you are violating const correctness. So for optimizations the compiler might rely on the _const_ attribute. That means bugs will depend on your level of optimization (i.e. in debug mode you application works find but crashes in non-debug mode). MTL4 Example ------------ :import: flens/examples/design-flaws-mtl4.cc [stripped, downloadable] Compile and Run --------------- *--[SHELL]-------------------------------------------------------------------* | | | cd flens/examples | | clang++ -I/opt/local/include/ -I$HOME/MTL/usr/include +++| | -o design-flaws-mtl4 +++| | design-flaws-mtl4.cc | | ./design-flaws-mtl4 | | | *----------------------------------------------------------------------------* :links: MTL4 -> http://www.simunova.com/de/node/65 :navigate: __up__ -> doc:flens/examples/tutorial __back__ -> doc:flens/examples/tut01-page06 __next__ -> doc:flens/examples/tut01-page08