Add boost::core::max_align(_t)

This commit is contained in:
Peter Dimov 2023-01-24 21:06:07 +02:00
parent d3ed836f75
commit 8052abb15c
3 changed files with 139 additions and 0 deletions

View File

@ -0,0 +1,84 @@
#ifndef BOOST_CORE_MAX_ALIGN_HPP_INCLUDED
#define BOOST_CORE_MAX_ALIGN_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/config.hpp>
#include <cstddef>
namespace boost
{
namespace core
{
union max_align_t
{
char c;
short s;
int i;
long l;
#ifndef BOOST_NO_LONG_LONG
boost::long_long_type ll;
#endif
#ifdef BOOST_HAS_INT128
boost::int128_type i128;
#endif
float f;
double d;
long double ld;
#ifdef BOOST_HAS_FLOAT128
__float128 f128;
#endif
void* p;
void (*pf) ();
int max_align_t::* pm;
void (max_align_t::*pmf)();
};
#if !defined(BOOST_NO_CXX11_ALIGNOF)
BOOST_CONSTEXPR_OR_CONST std::size_t max_align = alignof( max_align_t );
#elif defined(__GNUC__)
BOOST_CONSTEXPR_OR_CONST std::size_t max_align = __alignof__( max_align_t );
#elif defined(_MSC_VER)
BOOST_CONSTEXPR_OR_CONST std::size_t max_align = __alignof( max_align_t );
#else
namespace detail
{
struct alignment_of_helper
{
char x;
max_align_t a;
};
} // namespace detail
BOOST_CONSTEXPR_OR_CONST std::size_t max_align = offsetof( core::detail::alignment_of_helper, a );
#endif
} // namespace core
} // namespace boost
#endif // #ifndef BOOST_CORE_MAX_ALIGN_HPP_INCLUDED

View File

@ -354,5 +354,7 @@ run-fail verbose_terminate_handler_fail.cpp : : : <exception-handling>off <rtti>
run launder_test.cpp ;
run max_align_test.cpp ;
use-project /boost/core/swap : ./swap ;
build-project ./swap ;

53
test/max_align_test.cpp Normal file
View File

@ -0,0 +1,53 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/core/max_align.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/type_traits.hpp>
#include <boost/config.hpp>
#include <cstddef>
struct X
{
};
int main()
{
BOOST_TEST_EQ( boost::core::max_align, boost::alignment_of<boost::core::max_align_t>::value );
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<char>::value );
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<short>::value );
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<int>::value );
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<long>::value );
#ifndef BOOST_NO_LONG_LONG
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<boost::long_long_type>::value );
#endif
#ifdef BOOST_HAS_INT128
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<boost::int128_type>::value );
#endif
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<float>::value );
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<double>::value );
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<long double>::value );
#ifdef BOOST_HAS_FLOAT128
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<__float128>::value );
#endif
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<void*>::value );
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<void(*)()>::value );
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<int X::*>::value );
BOOST_TEST_GE( boost::core::max_align, boost::alignment_of<void (X::*)()>::value );
#if !defined(BOOST_NO_CXX11_ALIGNOF)
BOOST_TEST_EQ( boost::core::max_align, alignof( std::max_align_t ) );
#endif
return boost::report_errors();
}