support bazel complie this project and format code.

This commit is contained in:
zhangxing
2023-03-30 00:15:11 +08:00
committed by light-city
parent 1f86192576
commit 7529ae3a55
636 changed files with 10025 additions and 9387 deletions

View File

@@ -0,0 +1,27 @@
# please run `bazel run //learn_class/modern_cpp_30/compilercompute:IF`
# please run `bazel run //learn_class/modern_cpp_30/compilercompute:factorial`
# please run `bazel run //learn_class/modern_cpp_30/compilercompute:fmap`
# please run `bazel run //learn_class/modern_cpp_30/compilercompute:WhileLoop`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "IF",
srcs = ["IF.cpp"],
copts = ["-std=c++17"],
)
cc_binary(
name = "factorial",
srcs = ["factorial.cpp"],
copts = ["-std=c++17"],
)
cc_binary(
name = "fmap",
srcs = ["fmap.cpp"],
copts = ["-std=c++17"],
)
cc_binary(
name = "WhileLoop",
srcs = ["WhileLoop.cpp"],
copts = ["-std=c++17"],
)

View File

@@ -0,0 +1,43 @@
//
// Created by light on 19-12-28.
//
#include <iostream>
using namespace std;
// 使用template实现IF条件判断
template <bool cond, typename Then, typename Else> struct IF;
template <typename Then, typename Else> struct IF<true, Then, Else> {
typedef Then result;
};
template <typename Then, typename Else> struct IF<false, Then, Else> {
typedef Else result;
};
// 判断奇数与偶数
template <int N> struct isEven {
static const auto RES = IF<N & 1 == 0, true_type, false_type>::result::value;
};
template <int nums1, int nums2> struct Add_ {
static const int value = nums1 + nums2;
};
template <int nums1, int nums2> struct Sub_ {
static const int value = nums1 - nums2;
};
// 加减
template <bool cond, int nums1, int nums2> struct addSub {
static const auto RES =
IF<cond, Add_<nums1, nums2>, Sub_<nums1, nums2>>::result::value;
};
int main() {
cout << isEven<10>::RES << endl;
cout << addSub<true, 10, 2>::RES << endl;
}

View File

@@ -0,0 +1,57 @@
//
// Created by light on 20-1-5.
//
#include <iostream>
using namespace std;
// 使用template实现while循环
template <bool condition, typename Body> struct WhileLoop;
template <typename Body> struct WhileLoop<true, Body> {
typedef
typename WhileLoop<Body::cond_value, typename Body::next_type>::type type;
};
template <typename Body> struct WhileLoop<false, Body> {
typedef typename Body::res_type type;
};
template <typename Body> struct While {
typedef typename WhileLoop<Body::cond_value, Body>::type type;
};
template <typename Body> using While_t = WhileLoop<Body::cond_value, Body>;
namespace my {
template <class T, T v> struct integral_constant {
static const T value = v;
typedef T value_type;
typedef integral_constant<T, v> type;
};
} // namespace my
template <int result, int n> struct SumLoop {
// 循环的条件
static const bool cond_value = n != 0;
// 循环后的结果
static const int res_value = result;
// 循环时的状态
typedef my::integral_constant<int, res_value> res_type;
// 循环执行一次时的状态
typedef SumLoop<result + n, n - 1> next_type;
};
template <int n> struct Sum { typedef SumLoop<0, n> type; };
template <int n> using Sum_t = SumLoop<0, n>;
int main() {
cout << While<Sum<6>::type>::type::value << endl;
cout << While_t<Sum_t<6>>::type::value << endl;
return 0;
}

View File

@@ -0,0 +1,20 @@
//
// Created by light on 19-12-28.
//
#include <iostream>
using namespace std;
template <int n> struct factorial {
static_assert(n >= 0, "Arg must be non-negative");
static const int value = n * factorial<n - 1>::value;
};
template <> struct factorial<0> { static const int value = 1; };
int main() {
printf("%d\n", factorial<10>::value);
return 0;
}

View File

@@ -0,0 +1,34 @@
//
// Created by light on 20-1-5.
//
#include <iostream>
#include <vector>
using namespace std;
template <template <typename, typename> class OutContainer = vector, typename F,
class R>
auto fmap(F &&f, R &&inputs) {
typedef decay_t<decltype(f(*inputs.begin()))> result_type;
OutContainer<result_type, allocator<result_type>> result;
for (auto &&item : inputs) {
result.push_back(f(item));
}
return result;
}
// 对每一个数进行加1操作
int add_1(int x) { return x + 1; }
int main() {
vector<int> v{1, 2, 3, 4, 5};
auto result = fmap(add_1, v);
for (auto item : result)
cout << item << " ";
cout << endl;
return 0;
}