GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::data(3) C++ Standard Libary std::data(3)

std::data - std::data


Defined in header <array>
Defined in header <deque>
Defined in header <forward_list>
Defined in header <iterator>
Defined in header <list>
Defined in header <map>
Defined in header <regex>
Defined in header <set>
Defined in header <span> (since C++20)
Defined in header <string>
Defined in header <string_view>
Defined in header <unordered_map>
Defined in header <unordered_set>
Defined in header <vector>
template <class C> (1) (since C++17)
constexpr auto data(C& c) -> decltype(c.data());
template <class C> (2) (since C++17)
constexpr auto data(const C& c) -> decltype(c.data());
template <class T, std::size_t N> (3) (since C++17)
constexpr T* data(T (&array)[N]) noexcept;
template <class E> (4) (since C++17)
constexpr const E* data(std::initializer_list<E> il) noexcept;


Returns a pointer to the block of memory containing the elements of the range.


1,2) returns c.data()
3) returns array
4) returns il.begin()


c - a container or view with a data() member function
array - an array of arbitrary type
il - an initializer list


A pointer to the block of memory containing the elements of the range.


1) May throw implementation-defined exceptions.


The overload for std::initializer_list is necessary because it does not have a
member function data.


Feature-test macro: __cpp_lib_nonmember_container_access


template <class C>
constexpr auto data(C& c) -> decltype(c.data())
{
return c.data();
}


template <class C>
constexpr auto data(const C& c) -> decltype(c.data())
{
return c.data();
}
Third version
template <class T, std::size_t N>
constexpr T* data(T (&array)[N]) noexcept
{
return array;
}
Fourth version
template <class E>
constexpr const E* data(std::initializer_list<E> il) noexcept
{
return il.begin();
}

// Run this code


#include <string>
#include <cstring>
#include <iostream>


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;
}


Hello world!


ranges::data obtains a pointer to the beginning of a contiguous range
(C++20) (customization point object)
ranges::cdata obtains a pointer to the beginning of a read-only contiguous range
(C++20) (customization point object)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.