JC2 said:
[snip]
The real question is: What problem are you trying to solve? There is
likely a better way. This is a strange and unusual thing to want to
do.
[snip]
if I do
const Curve curve( begin iterator, end iterator );
I would then like to use for storage a container like boost::array with a
maximum compile-time known size
and where no data is added after construction, this would involve no dynamic
allocation whatsoever.
otherwise
Curve curve(begin iterator, end iterator )
I may then use vector<> for e.g., which is extensible but involves dynamic
allocation
First of all, this sounds like some premature optimization that will
likely not get you the performance benefits you are seeking. Does your
current implementation not meet your performance requirements? Are you
having performance issues that you need to solve? Is this really the
bottleneck? What you are attempting to do does not seem like a good
idea, it sounds like you may be headed down the wrong path.
Like Kai-Uwe Bux explained, if the size is known then there is only
one dynamic allocation on creation.
Also, I'm still not quite clear on what you are saying. Do you mean
that you would store Curve and const Curve in different containers,
e.g. vector<Curve> and array<const Curve,n>? Or do you mean that
within Curve, the storage you use to hold the data points is
different, e.g.:
class Curve {
...
vector<Point> points; // for non-const
array<Point,MAXSIZE> points; // for const
...
};
In both cases it's still a premature optimization with very little
effect. I'm assuming the latter case.
Basically you have two distinctly different types of Curves. If you
still insist on doing something (which IMHO is a bad idea), you could:
* Define a base Curve class, and derive ConstCurve and NonConstCurve
from it, with optimizations for the ConstCurve.
* Define a ConstCurve and NonConstCurve with the same interface and
use template tricks and specializations all around to optimize certain
operations for the ConstCurve case.
* Use Curve for the non-const case and simply use DataPoint[] arrays
for the const case, and overload time-critical functions with
optimized versions that accept DataPoint[] instead of Curve.