Skip to content

Instantly share code, notes, and snippets.

@shomah4a
Created March 26, 2012 04:45
Show Gist options
  • Save shomah4a/2202977 to your computer and use it in GitHub Desktop.
Save shomah4a/2202977 to your computer and use it in GitHub Desktop.
変数を使わないで 0 から 100 まで
#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