Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for a snapshot of the develop branch, built from commit 549cd0dc90.
PrevUpHomeNext

first_scalar

Overview
Examples
Reference
History

Authors

  • Glen Fernandes

The header <boost/core/first_scalar.hpp> provides the function templates boost::first_scalar that can be used to obtain a pointer to the first scalar element of an array. Given a pointer of type T* they return a pointer of type remove_all_extents_t<T>*. The functions are constexpr and can be used in constant expressions.

The following function uses an allocator to allocate an array of arrays and constructs each scalar element in it.

#include <boost/alloc_construct.hpp>
#include <boost/first_scalar.hpp>

template<class A>
auto create(const A& allocator)
{
    typename std::allocator_traits<A>::template
        rebind_alloc<int[2][3]> other(allocator);
    auto ptr = other.allocate(4);
    try {
        boost::alloc_construct_n(other,
            boost::first_scalar(boost::to_address(ptr)), 24);
    } catch (...) {
        other.deallocate(ptr, 4);
        throw;
    }
    return ptr;
}
namespace boost {

template<class T>
constexpr T* first_scalar(T* p) noexcept;

template<class T, std::size_t N>
constexpr auto first_scalar(T (*p)[N]) noexcept;

} /* boost */

template<class T> constexpr T* first_scalar(T* p) noexcept;

Returns

p.

template<class T, std::size_t N> constexpr auto first_scalar(T (*p)[N]) noexcept;

Returns

first_scalar(&(*p)[0]).

Glen Fernandes implemented first_scalar. Peter Dimov suggested a change for GCC to support an additional constexpr use.


PrevUpHomeNext