• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[/
2Copyright 2019 Glen Joseph Fernandes
3(glenjofe@gmail.com)
4
5Distributed under the Boost Software License, Version 1.0.
6(http://www.boost.org/LICENSE_1_0.txt)
7]
8
9[section:first_scalar first_scalar]
10
11[simplesect Authors]
12
13* Glen Fernandes
14
15[endsimplesect]
16
17[section Overview]
18
19The header <boost/core/first_scalar.hpp> provides the function templates
20`boost::first_scalar` that can be used to obtain a pointer to the first scalar
21element of an array. Given a pointer of type `T*` they return a pointer of
22type `remove_all_extents_t<T>*`. The functions are `constexpr` and can be used
23in constant expressions.
24
25[endsect]
26
27[section Examples]
28
29The following function uses an allocator to allocate an array of arrays and
30constructs each scalar element in it.
31
32```
33#include <boost/alloc_construct.hpp>
34#include <boost/first_scalar.hpp>
35
36template<class A>
37auto create(const A& allocator)
38{
39    typename std::allocator_traits<A>::template
40        rebind_alloc<int[2][3]> other(allocator);
41    auto ptr = other.allocate(4);
42    try {
43        boost::alloc_construct_n(other,
44            boost::first_scalar(boost::to_address(ptr)), 24);
45    } catch (...) {
46        other.deallocate(ptr, 4);
47        throw;
48    }
49    return ptr;
50}
51```
52
53[endsect]
54
55[section Reference]
56
57```
58namespace boost {
59
60template<class T>
61constexpr T* first_scalar(T* p) noexcept;
62
63template<class T, std::size_t N>
64constexpr auto first_scalar(T (*p)[N]) noexcept;
65
66} /* boost */
67```
68
69[section Functions]
70
71[variablelist
72[[`template<class T> constexpr T* first_scalar(T* p) noexcept;`]
73[[variablelist
74[[Returns][`p`.]]]]]
75[[`template<class T, std::size_t N> constexpr auto first_scalar(T (*p)[N])
76noexcept;`]
77[[variablelist
78[[Returns][`first_scalar(&(*p)[0])`.]]]]]]
79
80[endsect]
81
82[endsect]
83
84[section History]
85
86Glen Fernandes implemented `first_scalar`. Peter Dimov suggested a change for
87GCC to support an additional `constexpr` use.
88
89[endsect]
90
91[endsect]
92