Boost MPL bitwise not

ID: 31
creation date: 2016/04/22 15:14
modification date: 2016/04/22 15:14
owner: taiji
tags: C++, Boost MPL

Boost MPL を読んでいて気になったのですが、算術演算の「ビット否定」だけ、どう探しても実装されてないと思います。とは言え、自前で用意するのは非常に簡単。

namespace boost { namespace mpl {

  template <typename T>
  struct compl_ : integral_c<typename T::value_type, ~T::value> {};

} }

こういったものを含む boost/mpl/compl.hpp を用意すればよいでしょう。確認の仕方は、

  BOOST_MPL_ASSERT_RELATION((boost::mpl::compl_<boost::mpl::integral_c<unsigned, 0> >::value), ==, ~0U);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::compl_<boost::mpl::integral_c<unsigned, ~0U> >::value), ==, 0);

といった感じです。ちなみに、実装されている他のビット演算の確認の仕方は、

#include <boost/mpl/integral_c.hpp>
#include <boost/mpl/shift_left.hpp>
#include <boost/mpl/shift_right.hpp>
#include <boost/mpl/bitand.hpp>
#include <boost/mpl/bitxor.hpp>
#include <boost/mpl/bitor.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/assert.hpp>

必要なヘッダファイルをインクルードした上で、

  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitand_<boost::mpl::integral_c<unsigned, 0>, boost::mpl::integral_c<unsigned, 0> >::value), ==, 0U);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitand_<boost::mpl::integral_c<unsigned, 0>, boost::mpl::integral_c<unsigned, 1> >::value), ==, 0U);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitand_<boost::mpl::integral_c<unsigned, 1>, boost::mpl::integral_c<unsigned, 0> >::value), ==, 0U);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitand_<boost::mpl::integral_c<unsigned, 1>, boost::mpl::integral_c<unsigned, 1> >::value), ==, 1U);

  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitxor_<boost::mpl::integral_c<unsigned, 0>, boost::mpl::integral_c<unsigned, 0> >::value), ==, 0U);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitxor_<boost::mpl::integral_c<unsigned, 0>, boost::mpl::integral_c<unsigned, 1> >::value), ==, 1U);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitxor_<boost::mpl::integral_c<unsigned, 1>, boost::mpl::integral_c<unsigned, 0> >::value), ==, 1U);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitxor_<boost::mpl::integral_c<unsigned, 1>, boost::mpl::integral_c<unsigned, 1> >::value), ==, 0U);

  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitor_<boost::mpl::integral_c<unsigned, 0>, boost::mpl::integral_c<unsigned, 0> >::value), ==, 0U);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitor_<boost::mpl::integral_c<unsigned, 0>, boost::mpl::integral_c<unsigned, 1> >::value), ==, 1U);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitor_<boost::mpl::integral_c<unsigned, 1>, boost::mpl::integral_c<unsigned, 0> >::value), ==, 1U);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::bitor_<boost::mpl::integral_c<unsigned, 1>, boost::mpl::integral_c<unsigned, 1> >::value), ==, 1U);

  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_left<boost::mpl::integral_c<unsigned, 1>, boost::mpl::integral_c<unsigned, 0> >::value), ==, 1U<<0);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_left<boost::mpl::integral_c<unsigned, 1>, boost::mpl::integral_c<unsigned, 1> >::value), ==, 1U<<1);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_left<boost::mpl::integral_c<unsigned, 1>, boost::mpl::integral_c<unsigned, 2> >::value), ==, 1U<<2);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_left<boost::mpl::integral_c<unsigned, 1>, boost::mpl::integral_c<unsigned, 3> >::value), ==, 1U<<3);

  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_right<boost::mpl::integral_c<unsigned, ~0U>, boost::mpl::integral_c<unsigned, 0> >::value), ==, ~0U>>0);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_right<boost::mpl::integral_c<unsigned, ~0U>, boost::mpl::integral_c<unsigned, 1> >::value), ==, ~0U>>1);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_right<boost::mpl::integral_c<unsigned, ~0U>, boost::mpl::integral_c<unsigned, 2> >::value), ==, ~0U>>2);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_right<boost::mpl::integral_c<unsigned, ~0U>, boost::mpl::integral_c<unsigned, 3> >::value), ==, ~0U>>3);

  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_right<boost::mpl::integral_c<signed, ~0>, boost::mpl::integral_c<unsigned, 0> >::value), ==, ~0>>0);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_right<boost::mpl::integral_c<signed, ~0>, boost::mpl::integral_c<unsigned, 1> >::value), ==, ~0>>1);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_right<boost::mpl::integral_c<signed, ~0>, boost::mpl::integral_c<unsigned, 2> >::value), ==, ~0>>2);
  BOOST_MPL_ASSERT_RELATION((boost::mpl::shift_right<boost::mpl::integral_c<signed, ~0>, boost::mpl::integral_c<unsigned, 3> >::value), ==, ~0>>3);

というような感じです。

0 コメント
ゲストコメント認証用なぞなぞ:
キーボードのLから左に全部打って下さい。それを二回やって下さい。 ...