Skip to content

Instantly share code, notes, and snippets.

@drewxa
Created September 12, 2017 08:58
Show Gist options
  • Save drewxa/de63d3bd021029208efb70ab146ac7b9 to your computer and use it in GitHub Desktop.
Save drewxa/de63d3bd021029208efb70ab146ac7b9 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iomanip>
#include <cstdint>
#include <array>
using size_t = std::size_t;
// тут не шаблон, а auto в аргементах
auto simple_mult = [](const auto& a, const auto& b){return a*b;};
template <class T, class F = decltype(simple_mult)>
auto fast_pow(const T& a, size_t degree, F mult = simple_mult) // и тут <T> не нужен
{
auto power = a;
auto result = T(1);
while (degree){
if (degree & 1){
result = mult(result, power);
}
power = mult(power, power);
// а тут degree не изменялся
degree = degree >> 1;
}
return result;
}
int main() {
std::cout << "Hello, World!" << std::endl;
std::cout << fast_pow(2, 3) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment