mirror of
https://github.com/boostorg/core.git
synced 2025-05-11 13:13:55 +00:00
Add BOOST_CORE_ALIGNOF
This commit is contained in:
parent
642a0cf70e
commit
39cf1e65a3
57
include/boost/core/alignof.hpp
Normal file
57
include/boost/core/alignof.hpp
Normal file
@ -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 <boost/config.hpp>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
#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<class T> 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
|
@ -354,5 +354,7 @@ run-fail verbose_terminate_handler_fail.cpp : : : <exception-handling>off <rtti>
|
|||||||
|
|
||||||
run launder_test.cpp ;
|
run launder_test.cpp ;
|
||||||
|
|
||||||
|
run alignof_test.cpp ;
|
||||||
|
|
||||||
use-project /boost/core/swap : ./swap ;
|
use-project /boost/core/swap : ./swap ;
|
||||||
build-project ./swap ;
|
build-project ./swap ;
|
||||||
|
88
test/alignof_test.cpp
Normal file
88
test/alignof_test.cpp
Normal file
@ -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 <boost/core/alignof.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
#include <boost/type_traits.hpp>
|
||||||
|
#include <boost/config.hpp>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
template<class T> struct struct_of
|
||||||
|
{
|
||||||
|
T t;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T> union union_of
|
||||||
|
{
|
||||||
|
T t;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T> void test2()
|
||||||
|
{
|
||||||
|
BOOST_TEST_EQ( BOOST_CORE_ALIGNOF(T), boost::alignment_of<T>::value );
|
||||||
|
|
||||||
|
#if !defined(BOOST_NO_CXX11_ALIGNOF)
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( BOOST_CORE_ALIGNOF(T), alignof(T) );
|
||||||
|
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T> void test()
|
||||||
|
{
|
||||||
|
test2<T>();
|
||||||
|
test2<T[2]>();
|
||||||
|
test2< struct_of<T> >();
|
||||||
|
test2< union_of<T> >();
|
||||||
|
}
|
||||||
|
|
||||||
|
struct X
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
test<char>();
|
||||||
|
test<short>();
|
||||||
|
test<int>();
|
||||||
|
test<long>();
|
||||||
|
|
||||||
|
#if !defined(BOOST_NO_LONG_LONG)
|
||||||
|
|
||||||
|
test<boost::long_long_type>();
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(BOOST_HAS_INT128)
|
||||||
|
|
||||||
|
test<boost::int128_type>();
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
test<float>();
|
||||||
|
test<double>();
|
||||||
|
test<long double>();
|
||||||
|
|
||||||
|
#if defined(BOOST_HAS_FLOAT128)
|
||||||
|
|
||||||
|
test<__float128>();
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
test<void*>();
|
||||||
|
test<void(*)()>();
|
||||||
|
|
||||||
|
#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<int X::*>();
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
test<void (X::*)()>();
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user