Last active
October 25, 2018 20:54
-
-
Save ahmedalkabir/048e8e18694f0146f26fe4e8ffd14994 to your computer and use it in GitHub Desktop.
Simple version of metafunction that converts Binary to Decimal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
// Improved version of metafunction that convert | |
// Binary to Decimal | |
// | |
template<unsigned long N> | |
struct B_TO_D{ | |
static unsigned constexpr value = B_TO_D<N/10>::value << 1 | N%10; // prepend higher bits to lowest bit | |
}; | |
template<> | |
struct B_TO_D<0> | |
{ | |
static unsigned constexpr value = 0; | |
}; | |
int main(){ | |
auto decimal = B_TO_D<1010>::value; | |
std::cout << decimal << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment