Does using std::array lead to code bloat? [duplicate]

I wouldn’t worry about it. If you look at the interface of std::array<T, N>, it is very small and most member functions (basically providing wrappers for pointer manipulation) are one-liners that will be completely optimized away / inlined by any decent compiler on Release mode optimization levels.

Furthermore, you don’t pay for what you don’t use since unused non-virtual member functions (std::array<T, N> does not have virtual member functions) of class templates are guaranteed not to be instantiated. Small Standard quote:

14.7.1 Implicit instantiation [temp.inst]

11 An implementation shall not implicitly instantiate a function
template, a variable template, a member template, a non-virtual member
function, a member class, or a static data member of a class template
that does not require instantiation. […]

There are also some overloaded relational operators == and < that are semantically equivalent to std::equal and std::lexicographical_compare. In practice, these operators should also be implemented in terms of these algorithms (complain to your vendor if they don’t).

The only very small worry is a little extra compile-time overhead, but there should be zero code-size and run-time overhead.

Related but not identical: the Technical Report on C++ Performance did a lot of careful benchmarks on thin class wrappers around builtin types (int, double) and found close to zero overhead for 2006 compiler technology. You could repeat their testing to verify this for std::array<T,N> vs. T[N]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)