centralized performance resources
This commit is contained in:
commit
50b15a1522
63 changed files with 328466 additions and 0 deletions
48
perf-cpp/scripts/ch11.cc
Normal file
48
perf-cpp/scripts/ch11.cc
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
Write a constexpr function template with a non-type template parameter that
|
||||
returns the factorial of the template argument. The following program should
|
||||
fail to compile when it reaches factorial<-3>().
|
||||
|
||||
*/
|
||||
|
||||
template <int N>
|
||||
constexpr long long fibonacci() {
|
||||
static_assert(N >= 0);
|
||||
if constexpr (N <= 1) {
|
||||
return N;
|
||||
} else {
|
||||
return fibonacci<N - 1>() + fibonacci<N - 2>();
|
||||
}
|
||||
}
|
||||
|
||||
// int main() {
|
||||
// static_assert(fibonacci<1LL>() == 1);
|
||||
// // static_assert(factorial<3>() == 6);
|
||||
// // static_assert(factorial<5>() == 120);
|
||||
//
|
||||
// // fibonacci<-3>();
|
||||
//
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
#include <iostream>
|
||||
|
||||
template <typename T>
|
||||
T add(T x, T y) {
|
||||
return x + y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T mult(T t, int N) {
|
||||
return t * N;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << add(2, 3) << '\n';
|
||||
std::cout << add(1.2, 3.4) << '\n';
|
||||
|
||||
std::cout << mult(2, 3) << '\n';
|
||||
std::cout << mult(1.2, 3) << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue