Editing CPPTM Answers - Exercise 3-0
BOOST WIKI
|
RecentChanges
|
Preferences
|
Page List
|
Links List
// (by Ariel Badichi) #include <iostream> #include <boost/static_assert.hpp> template<unsigned int N> struct binary { static const unsigned int digit = N % 10; BOOST_STATIC_ASSERT(digit == 0 || digit == 1); static const unsigned int value = digit + (binary<N / 10>::value * 2); }; template<> struct binary<0> { static const unsigned int value = 0; }; int main() { // add a 2 and see what happens ;) std::cout << binary<101010>::value << "\n"; return 0; } -------------------- my answer, similar but different (note: this editor is messing with my text so look at this in the editor as it does not appear that way on the site)... #include <boost/static_assert.hpp> #include <boost/mpl/int.hpp> #ifdef NDEBUG # define CTASSERT(x) #else # define CTASSERT(x) BOOST_STATIC_ASSERT(x) #endif template<unsigned long N> class binary: public boost::mpl::int_<boost::mpl::int_<binary<N/10>::value>::value<<1 | N%10> { CTASSERT((N%10 & ~1) == 0); }; template<> class binary<0>: public boost::mpl::int_<0> { };
Summary:
This change is a minor edit.
(Visit
Preferences
to set your user name.)
View other revisions
BOOST WIKI
|
RecentChanges
|
Preferences
|
Page List
|
Links List
Disclaimer: This site not officially maintained by Boost Developers