Site icon Learn 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:

[crayon-673f6cca282dc525842104/]

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

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:

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.:

[crayon-673f6cca282e5072004683/]

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.

Exit mobile version