Created
March 26, 2012 04:45
-
-
Save shomah4a/2202977 to your computer and use it in GitHub Desktop.
変数を使わないで 0 から 100 まで
This file contains hidden or 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> | |
template <int start, int end> | |
class Sum | |
{ | |
template <int _start, int _end, int _result> | |
class Internal | |
{ | |
public: | |
static const int value = Internal<_start, _end-1, _result+_end-1>::value; | |
}; | |
template <int _start, int _result> | |
class Internal<_start, _start, _result> | |
{ | |
public: | |
static const int value = _result; | |
}; | |
template <int _start, int _end, bool result> | |
class Compare; | |
template <int _start, int _end> | |
class Compare<_start, _end, true> | |
{ | |
public: | |
static const int value = Internal<_start, _end, _end>::value; | |
}; | |
template <int _start, int _end> | |
class Compare<_start, _end, false> | |
{ | |
public: | |
static const int value = Internal<_end, _start, _start>::value; | |
}; | |
public: | |
static const int value = Compare<start, end, start<end>::value; | |
}; | |
int main(const int argc, const char* const args[]) | |
{ | |
std::cout << Sum<0, 100>::value << std::endl; | |
std::cout << Sum<100, 0>::value << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment