The (accepted) proposal for "Runtime-sized arrays with automatic storage duration (N3639)" asserts that
Stack overflow becomes more likely, in particular if the size depends on external input and is not properly checked. Some environments might therefore prohibit the use of the feature. Such a prohibition can be easily enforced with a static analysis tool.
I do not consider enforcement to be easy if it requires the analyzer to implement a full C++ compiler.
Consider the following code:
template<typename T>
inline void array_user( const T& x )
{
int a[f(traits<T>::omega)];
}
It looks to me like the analysis needs to be repeated for each use of array_user<T> and consider:
- Applicable specializations of
traits<T>discoverable at the point of use ofarray_user<T> - Whether
traits<T>::omegais a compile-time constant expression (either viaconstexpror C++03 approaches such asenum) - The type of
traits<T>::omega - Whether the applicable overload of
f()(at the point of use ofarray_user<T>and possibly found via ADL) isconstexpr
Am I missing something? Is it possible to enforce such a restriction without going through full compilation?
Can code be written in such a way to simplify verification of non-use of runtime bounds?
constexprfunctions in C++1y, writing a general static analyzer will require you to implement an interpreter as well as a compiler.constexprfunction will return a compile-time constant if its parameters are compile-time constants? Or can it depend on specializations of templates consumed (possibly indirectly) by theconstexprfunction? I still feel like that can be determined via type analysis, without actually computing the result of theconstexprfunction (and all the execution that entails)constexprfunctions are only guaranteed to be evaluated at compile time when used in a context that requires a constant expression - I'm certain how they interact with arrays of runtime bound. So it may be necessary to determine if the arguments are constant expressions to determine if an array has static or runtime bound.constexprfunctions at compile-time. In C++14, would the compiler be able to choose to make an array of runtime bound even when a statically-sized array would be feasible, and then evaluateconstexprfunctions at runtime?