프로그래밍 검색 블로그

템플릿 메타 프로그래밍 binary 본문

연습장

템플릿 메타 프로그래밍 binary

코딩조무사 2017. 10. 5. 22:37
1
2
3
4
5
6
7
8
9
10
11
12
template<unsigned long long N>
struct binary{
    static const unsigned long long value = binary<N % 10LL>::value + 2 * binary</ 10LL>::value;
};
template<>
struct binary<1LL>{
    static const unsigned long long value = 1;
};
template<>
struct binary<0LL>{
    static const unsigned long long value = 0;
};
cs





테스트


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main(){
    cout << binary<0>::value << endl;
    cout << binary<1>::value << endl;
    cout << binary<10>::value << endl;
    cout << binary<11>::value << endl;
    cout << binary<100>::value << endl;
    cout << binary<101>::value << endl;
    cout << binary<110>::value << endl;
    cout << binary<111>::value << endl;
    cout << binary<1000>::value << endl;
    cout << binary<1001>::value << endl;
    cout << binary<1010>::value << endl;
    
    //error
    //cout << binary<2>::value << endl;
}
cs


Comments