C++17Generic ProgrammingLearn C++

Helpers for Generic C++ Code Accessing Container Properties

In addition to Helpers for Generic C++ Code Iterating Over Sequences the C++17 Standard Library provides the following helpers for writing generic code:

Function std::data() provides access to the underlying raw data of:

  • containers that have a member function data();
  • instances of type type std::initializer_list<T>;
  • built-in arrays.

Function std::empty() yields true if either container or an instance of type std::initializer_list<T> is empty. For built-in arrays the returned value is always false because built-int arrays cannot be zero-sized according to the standard.

Function std::size() returns a number of elements of:

  • containers which are provides an iterator interface;
  • built-in arrays.

Please note, that std::size() eliminates the need of use the conservative way to calculate the number of elements of built-in arrays, i.e.:

Finally, please note, that being used with containers these functions just calls the corresponding member functions: data(), empty() and size(). Thus, for instance, std::size() does not work with instances of type std::forward_list<T> since this class doesn’t have the member function size() by design.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

Related posts
C++C++17C++20Introduction to C++Learn C++

When Is The CMath Mathematical Special Functions Header Used in Modern C++?

C++C++17C++20Introduction to C++Learn C++

What Are The CMath Mathematical Special Functions in Modern C++?

C++C++17C++20Learn C++

What Are The New Rules For Auto Deduction In C++ 17?

C++C++14C++17Learn C++Syntax

What Are The New Overloads For Ranges In C++ 14?