Aug 03, 2018 10:56
Итак, задача:
Sum of naturals divisible by 3 and 5
Write a program that calculates and prints the sum of all the natural numbers divisible by either 3 or 5, up to a given limit entered by the user.
Решение:
#include
// if operator for templates
template
struct If;
template
struct If {
enum { RESULT = ThenValue };
};
template
struct If {
enum { RESULT = ElseValue };
};
// solution
template
struct Sum {
enum {
RESULT = If::RESULT
+ Sum::RESULT
};
};
// endstop
template<>
struct Sum<0> {
enum { RESULT = 0 };
};
int main() {
std::cout << "sum = " << Sum<100>::RESULT;
return 0;
}
метапрограммирование,
задача,
с++,
#include