std::data - cppreference.com
From cppreference.com
| Defined in header |
||
| Defined in header |
||
| Defined in header |
(since C++23) |
|
| Defined in header |
(since C++23) |
|
| Defined in header |
||
| Defined in header |
(since C++26) |
|
| Defined in header |
||
| Defined in header |
||
| Defined in header |
||
| Defined in header |
(since C++26) |
|
| Defined in header |
||
| Defined in header |
||
| Defined in header |
(since C++20) |
|
| Defined in header |
(since C++23) |
|
| Defined in header |
||
| Defined in header |
||
| Defined in header |
||
| Defined in header |
||
| Defined in header |
||
| Defined in header |
||
template< class C > constexpr auto data( C& c ) noexcept(noexcept(c.data())) -> decltype(c.data()); |
(1) | (since C++17) |
template< class C > constexpr auto data( const C& c ) noexcept(noexcept(c.data())) -> decltype(c.data()); |
(2) | (since C++17) |
template< class T, std::size_t N > constexpr T* data( T (&array)[N] ) noexcept; |
(3) | (since C++17) |
Returns a pointer to the block of memory containing the elements of the range.
1,2) Returns c.data().
3) Returns array.
Parameters
| c | - | a container or view with a data() member function
|
| array | - | an array of arbitrary type |
Return value
1,2) c.data()
3) array
Exceptions
1,2) What and when the underlying c.data() call throws.
Notes
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_nonmember_container_access |
201411L |
(C++17) | std::size(), std::data(), and std::empty()
|
__cpp_lib_initializer_list |
202511L |
(C++26) (DR11) |
data and empty member functions of std::initializer_list;removing unnecessary free functions for std::initializer_list[1]
|
Possible implementation
| First version |
|---|
template<class C> constexpr auto data(C& c) noexcept(noexcept(c.data())) -> decltype(c.data()) { return c.data(); } |
| Second version |
template<class C> constexpr auto data(const C& c) noexcept(noexcept(c.data())) -> decltype(c.data()) { return c.data(); } |
| Third version |
template<class T, std::size_t N> constexpr T* data(T (&array)[N]) noexcept { return array; } |
Example
#include <cstring> #include <iostream> #include <string> int main() { std::string s{"Hello world!\n"}; char a[20]; // storage for a C-style string std::strcpy(a, std::data(s)); // [s.data(), s.data() + s.size()] is guaranteed to be an NTBS since C++11 std::cout << a; }
Output:
Defect report
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| P3016R6 | C++17 | 1) non-member std::data overload was provided for std::initializer_list2) std::data was not required to propagate exception specification
|
1) removed 2) required |
See also
| obtains a pointer to the beginning of a contiguous range (customization point object)[edit] | |
| obtains a pointer to the beginning of a read-only contiguous range (customization point object)[edit] |