diff --git a/include/boost/core/alignof.hpp b/include/boost/core/alignof.hpp new file mode 100644 index 0000000..de1f01c --- /dev/null +++ b/include/boost/core/alignof.hpp @@ -0,0 +1,57 @@ +#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 +#include + +#if !defined(BOOST_NO_CXX11_ALIGNOF) + +#define BOOST_CORE_ALIGNOF alignof + +#elif defined(__GNUC__) + +#define BOOST_CORE_ALIGNOF __alignof__ + +#elif defined(_MSC_VER) + +#define BOOST_CORE_ALIGNOF __alignof + +#else + +namespace boost +{ +namespace core +{ +namespace detail +{ + +template struct alignof_helper +{ + char x; + T t; +}; + +} // namespace detail +} // namespace core +} // namespace boost + +#if defined(__GNUC__) +// ignoring -Wvariadic-macros with #pragma doesn't work under GCC +# pragma GCC system_header +#endif + +#define BOOST_CORE_ALIGNOF(...) offsetof( ::boost::core::detail::alignof_helper<__VA_ARGS__>, t ); + +#endif + +#endif // #ifndef BOOST_CORE_MAX_ALIGN_HPP_INCLUDED diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 3b1b5a1..72804c3 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -354,5 +354,7 @@ run-fail verbose_terminate_handler_fail.cpp : : : off run launder_test.cpp ; +run alignof_test.cpp ; + use-project /boost/core/swap : ./swap ; build-project ./swap ; diff --git a/test/alignof_test.cpp b/test/alignof_test.cpp new file mode 100644 index 0000000..d5b0241 --- /dev/null +++ b/test/alignof_test.cpp @@ -0,0 +1,88 @@ +// Copyright 2023 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +template struct struct_of +{ + T t; +}; + +template union union_of +{ + T t; +}; + +template void test2() +{ + BOOST_TEST_EQ( BOOST_CORE_ALIGNOF(T), boost::alignment_of::value ); + +#if !defined(BOOST_NO_CXX11_ALIGNOF) + + BOOST_TEST_EQ( BOOST_CORE_ALIGNOF(T), alignof(T) ); + +#endif +} + +template void test() +{ + test2(); + test2(); + test2< struct_of >(); + test2< union_of >(); +} + +struct X +{ +}; + +int main() +{ + test(); + test(); + test(); + test(); + +#if !defined(BOOST_NO_LONG_LONG) + + test(); + +#endif + +#if defined(BOOST_HAS_INT128) + + test(); + +#endif + + test(); + test(); + test(); + +#if defined(BOOST_HAS_FLOAT128) + + test<__float128>(); + +#endif + + test(); + test(); + +#if !defined(_MSC_VER) + + // under MSVC, alignof is 8, boost::alignment_of is 4 + // under clang-cl, alignof is 4, boost::alignment_of is 8 (!) + + test(); + +#endif + + test(); + + return boost::report_errors(); +}