をジャストオペレータ+戻り値の型に新しいタイプを計算し、比較のためにMPLを使用 また、あなたがそのために友人を必要としないでもないあなたのオペレータ+可変にする必要があります。簡単なクラスと
簡単な例:
今
#include <boost/mpl/max.hpp>
template<unsigned char B, unsigned char F>
class Foo
{};
template< unsigned char B1, unsigned char F1
, unsigned char B2, unsigned char F2
>
Foo< boost::mpl::max_<boost::mpl_::char_<B1>, boost::mpl_::char_<B2> >::value
, boost::mpl::max_<boost::mpl_::char_<F1>, boost::mpl_::char_<F2> >::value
>
operator+(Foo<B1,F1> const& a, Foo<B2,F2> const& b)
{
Foo< boost::mpl::max_<boost::mpl_::char_<B1>, boost::mpl_::char_<B2> >::value
, boost::mpl::max_<boost::mpl_::char_<F1>, boost::mpl_::char_<F2> >::value
> that;
return that;
}
は、それが面倒であるかに気づきます。 また、これはpre_C++ 11自動で書き留めて少し複雑に+の戻り値の型を作る:この場合、通常のイディオムがUEにあるEDITたび
#include <boost/mpl/max.hpp>
template<class B, class F>
class Foo
{};
template< class B1, class F1
, class B2, class F2
>
Foo< typename boost::mpl::max_<B1, B2>::type
, typename boost::mpl::max_<F1, F2>::type
>
operator+(Foo<B1,F1> const& a, Foo<B2,F2> const& b)
{
Foo< typename boost::mpl::max_<B1, B2>::type
, typename boost::mpl::max_<F1, F2>::type
> that;
return that;
}
Foo< boost::mpl::int_<4>, boost::mpl::int_<8> > x;
可能代わりに生の値のMPL整数型を後押し。 もう一つの古典的なことは、これをプロトコルのresult_ofに続く関数オブジェクトにすることです。そのため、戻り値の型を不透明な方法で計算するのに便利なメタ関数があります。
.... T T ::演算子+(のconst T&B)のconst; - http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B –
可能な複製http://stackoverflow.com/q/4421706/1155650 –
私のポスターが正しく表示されない –