Skip to content

Instantly share code, notes, and snippets.

@ahmedalkabir
Last active October 25, 2018 20:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedalkabir/048e8e18694f0146f26fe4e8ffd14994 to your computer and use it in GitHub Desktop.
Save ahmedalkabir/048e8e18694f0146f26fe4e8ffd14994 to your computer and use it in GitHub Desktop.
Simple version of metafunction that converts Binary to Decimal
#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