Moved detail::length() implementation to avoid forward reference