Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constexpr conversion to MPL types #134

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions include/boost/type_traits/integral_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ namespace boost{
typedef integral_constant<T, val> type;
static const T value = val;

operator const mpl::integral_c<T, val>& ()const
{
static const char data[sizeof(long)] = { 0 };
static const void* pdata = data;
return *(reinterpret_cast<const mpl::integral_c<T, val>*>(pdata));
}
BOOST_CONSTEXPR operator mpl::integral_c<T, val>()const
{ return mpl::integral_c<T, val>(); }
BOOST_CONSTEXPR operator T()const { return val; }
};

Expand All @@ -77,12 +73,8 @@ namespace boost{
typedef integral_constant<bool, val> type;
static const bool value = val;

operator const mpl::bool_<val>& ()const
{
static const char data[sizeof(long)] = { 0 };
static const void* pdata = data;
return *(reinterpret_cast<const mpl::bool_<val>*>(pdata));
}
BOOST_CONSTEXPR operator mpl::bool_<val>()const
{ return mpl::bool_<val>(); }
BOOST_CONSTEXPR operator bool()const { return val; }
};

Expand Down
25 changes: 20 additions & 5 deletions test/mpl_interop_test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,41 @@
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/type_traits/is_void.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/config.hpp>

template <class T>
int dispatch_test_imp(const boost::mpl::bool_<true>&)
BOOST_CONSTEXPR int dispatch_test_imp1(const boost::mpl::bool_<true>&)
{
return 0;
}
template <class T>
int dispatch_test_imp(const boost::mpl::bool_<false>&)
BOOST_CONSTEXPR int dispatch_test_imp1(const boost::mpl::bool_<false>&)
{
return 1;
}

template <class T>
int dispatch_test()
BOOST_CONSTEXPR int dispatch_test_imp2(boost::mpl::true_)
{
return dispatch_test_imp<T>(boost::is_void<T>());
return 0;
}
template <class T>
BOOST_CONSTEXPR int dispatch_test_imp2(boost::mpl::false_)
{
return 1;
}

template <class T>
BOOST_CONSTEXPR int dispatch_test()
{
return dispatch_test_imp1<T>(boost::is_void<T>()) +
dispatch_test_imp2<T>(boost::is_void<T>());
}


int main()
{
return (dispatch_test<int>() == 1) && (dispatch_test<void>() == 0) ? 0 : 1;
BOOST_CONSTEXPR bool result = (dispatch_test<int>() == 2) && (dispatch_test<void>() == 0);
return result ? 0 : 1;
}