1/*
2 * Copyright Andrey Semashev 2014.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
6 */
7/*!
8 * \file underlying_type.cpp
9 * \author Andrey Semashev
10 * \date 06.06.2014
11 *
12 * \brief This test checks that underlying_type trait works.
13 */
14
15#include <boost/core/underlying_type.hpp>
16#include <boost/core/scoped_enum.hpp>
17#include <boost/core/lightweight_test_trait.hpp>
18#include <boost/type_traits/is_same.hpp>
19#include <boost/config.hpp>
20
21#if defined(_MSC_VER)
22# pragma warning(disable: 4244) // conversion from enum_type to underlying_type
23#endif
24
25BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(emulated_enum, unsigned char)
26{
27 value0,
28 value1,
29 value2
30}
31BOOST_SCOPED_ENUM_DECLARE_END(emulated_enum)
32
33#if !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
34
35enum class native_enum : unsigned short
36{
37 value0,
38 value1,
39 value2
40};
41
42#endif
43
44#if defined(BOOST_NO_UNDERLYING_TYPE)
45namespace boost {
46
47template< >
48struct underlying_type< emulated_enum >
49{
50 typedef unsigned char type;
51};
52
53#if !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
54template< >
55struct underlying_type< native_enum >
56{
57 typedef unsigned short type;
58};
59#endif
60
61} // namespace boost
62#endif
63
64int main(int, char*[])
65{
66 BOOST_TEST_TRAIT_TRUE((boost::is_same< boost::underlying_type< emulated_enum >::type, unsigned char >));
67#if !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
68 BOOST_TEST_TRAIT_TRUE((boost::is_same< boost::underlying_type< native_enum >::type, unsigned short >));
69#endif
70
71 return boost::report_errors();
72}
73

source code of boost/libs/core/test/underlying_type.cpp