Has anyone looked into adding support for a const Array type to the blitz++ package? Simply qualifying an Array instance as const is not completely safe because it is very easy to create a reference that losses the const qualification. I have not done any feasibility work for this enhancement. Before I begin, I wanted to see if anyone has given this some thought.
For example:
const Array<int,2> array(4,4);
/* Not allowed because array is qualified as const, good
array = 1, 2, 3, 4, ...
*/
// Can only get data() as const, good
// int* data = array.data();
const int* data = array.data();
// Make an array that references the const qualified array,
// in this case via slice. The 'slice' array can modify the
// const qualified array data.
Array<int,1> slice = array(1,Range::all());
// data is not changes, bad
slice = 1,2,3,4;
The only solution that comes to mind is to use a const array type. The following example is adding an additional template argument of an enum type to specify if the Array is Mutable (default), or Constant. For example:
// type is Constant
Array<int,2,Constant> array(4,4);
/* Not allowed on Constant type, good
array = 1, 2, 3, 4, ...
*/
// Can only get data() as const, good
// int* data = array.data();
const int* data = array.data();
// Only return references that are also of Constant type
//Array<int,1> slice = array(1,Range::all()); not allowed, good
Array<int,1,Constant> slice = array(1,Range::all());
// slice = 1,2,3,4; not allowed, good
The motivation behind using template argument of an enum type is to use Mutable as the default which would allow all existing code to work without change. Would also impact constructors, i.e. can create a Constant Array from any Array, but cannot create a Mutable Array from a Constant Array.
Following up on a post on the mailing list: https://sourceforge.net/p/blitz/mailman/message/32391243/
Has anyone looked into adding support for a const Array type to the blitz++ package? Simply qualifying an Array instance as const is not completely safe because it is very easy to create a reference that losses the const qualification. I have not done any feasibility work for this enhancement. Before I begin, I wanted to see if anyone has given this some thought.
For example:
The only solution that comes to mind is to use a const array type. The following example is adding an additional template argument of an enum type to specify if the Array is Mutable (default), or Constant. For example:
The motivation behind using template argument of an enum type is to use Mutable as the default which would allow all existing code to work without change. Would also impact constructors, i.e. can create a Constant Array from any Array, but cannot create a Mutable Array from a Constant Array.
Regards,
Matt