- 实现加一
#include
template<int x>
struct M
{
constexpr static int val = x + 1;
};
int main()
{
std::cout << M<4>::val << std::endl;
}
- 用编译期函数实现加一
#include
constexpr int add_fun(int x) {
return x + 1;
}
constexpr int val = add_fun(5);
int main(){
std::cout << val << std::endl;
}
- 去掉变量的引用,并加上const修饰
#include
#include
template<typename T>
struct Fun
{
using RemDef = typename std::remove_reference<T>::type;
using type = typename std::add_const<RemDef>::type;
};
int main()
{
Fun<int&>::type x = 3;
std::cout << std::is_const_v<Fun<int&>::type> << std::endl;
std::cout << std::is_reference_v<Fun<int&>::type> << std::endl;
}
- 计算给定类型是否是指定长度字节
#include
#include
template<typename T,int S>
struct Fun2
{
constexpr static bool value = (sizeof(T) == S);
};
int main()
{
constexpr bool res = Fun2<int, 4>::value;
std::cout << res << std::endl;
}
- factorial求和
#include
template<unsigned int n>
struct factorial
{
constexpr static int value = n * factorial<n - 1>::value;
};
template<>
struct factorial<0>
{
constexpr static int value = 1;
};
int main()
{
std::cout << factorial<7>::value << std::endl;
}
- 将数组里的元素求和(可变模板参数求和)
#include
template <int... inputs>
struct Accumulate {
constexpr static int value = 0;
};
template <int CurInput,int... Inputs>
struct Accumulate <CurInput,Inputs...>{
constexpr static int value = CurInput + Accumulate<Inputs...>::value;
};
int main()
{
constexpr static int res_accumulate = Accumulate<1,2,3,4,5,6>::value;
std::cout << res_accumulate << std::endl;
}
- 打印类似Cont<1,2,3>的数组,打印可变模板参数
#include
template<unsigned int... T>
struct Cont_arr {};
void print_list(Cont_arr<>) {};
template<unsigned int N, unsigned int... T>
void print_list(Cont_arr<N, T...>) {
std::cout << N << std::endl;
print_list(Cont_arr<T...>{});
}
int main()
{
print_list(Cont_arr<1, 2 ,3>{});
}
- 判断一个数是不是质数,以下的写法在c++98也能使用,书上抄的例子
#include
template<unsigned p, unsigned d>
struct DoIsPrime {
static const bool value = (p % d != 0) && DoIsPrime<p, d - 1>::value;
};
template<unsigned p>
struct DoIsPrime<p, 2>
{
static const bool value = (p % 2 != 0);
};
template<unsigned p>
struct IsPrime
{
static const bool value = DoIsPrime<p, p / 2>::value;
};
int main()
{
auto value = IsPrime<5>::value;
std::cout << value << std::endl;
auto value2 = IsPrime<6>::value;
std::cout << value2 << std::endl;
}
- SFINAE(发音类似 sfee-nay)
SFINAE,用一个求长度的例子len来实现,也是书上的例子
#include
#include
template<typename T,unsigned N>
std::size_t len(T(&)[N])
{
return N;
};
template<typename T>
typename T::size_type len(T const& t) {
return t.size();
};
std::size_t len(...)
{
return 0;
}
int main()
{
std::cout << len("123") << std::endl;
std::cout << len(std::vector<int>(4) ) <<std::endl;
int* p = nullptr;
std::cout << len(p);
}
- 翻转数列
#include
template<unsigned... N> struct Cont {};
template<typename T> struct Reverse;
template<typename T, typename P, size_t S> struct do_reverse;
template <unsigned... L>
struct Reverse<Cont<L...>> {
using type = typename do_reverse<Cont<>, Cont<L...>, sizeof...(L)>::value;
};
template <unsigned N, unsigned... L1, unsigned... L2>
struct do_reverse<Cont<L2...>, Cont<N, L1...>, 1> {
using value = Cont<N, L2...>;
};
template <unsigned N, size_t S, unsigned... L1, unsigned... L2>
struct do_reverse<Cont<L2...>, Cont<N, L1...>, S> {
using value = typename do_reverse<Cont<N, L2...>, Cont<L1...>, (S - 1)>::value;
};
template<unsigned ...x>
void print_list(Cont<x...>) {
((std::cout << x << ","), ...);
}
int main()
{
print_list(Reverse<Cont<1, 2, 3, 4, 5>>::type{});
}
- 数列相加
#include
template<unsigned int... T>
struct Cont {};
template<typename T, typename P> struct Add;
template<typename T, typename P, typename Q, unsigned S> struct do_add;
template<unsigned ...L1, unsigned... L2>
struct Add<Cont<L1...>, Cont<L2...>> {
using type = typename do_add<Cont<>, Cont<L1...>, Cont<L2...>, 0>::value;
};
template<unsigned... L3, unsigned... L1, unsigned... L2, unsigned N1, unsigned N2, unsigned S>
struct do_add<Cont<L3...>, Cont<N1, L1...>, Cont<N2, L2...>, S> {
using value = typename do_add < Cont< L3..., ((N1 + N2 + S) % 10) >, Cont<L1...>, Cont<L2...>, ((N1 + N2 + S) / 10) > ::value;
};
template<unsigned... L3, unsigned... L1, unsigned N1, unsigned S>
struct do_add<Cont<L3...>, Cont<N1, L1...>, Cont<>, S> {
using value = typename do_add < Cont<L3..., ((N1 + S) % 10) >, Cont<L1...>, Cont<>, ((N1 + S) / 10) > ::value;
};
template<unsigned... L3, unsigned... L2, unsigned N2, unsigned S>
struct do_add<Cont<L3...>, Cont<>, Cont<N2, L2...>, S> {
using value = typename do_add < Cont < L3..., ((N2 + S) % 10) >, Cont<>, Cont<L2...>, ((N2 + S) / 10) > ::value;
};
template<unsigned... L3>
struct do_add<Cont<L3...>, Cont<>, Cont<>, 1> {
using value = Cont<L3..., 1>;
};
template<unsigned... L3>
struct do_add<Cont<L3...>, Cont<>, Cont<>, 0> {
using value = Cont<L3...>;
};
void print_list(Cont<>) {};
template<unsigned int N, unsigned int... T>
void print_list(Cont<N, T...>) {
std::cout << N << std::endl;
print_list(Cont<T...>{});
}
int main()
{
print_list(Add<Cont<1, 2, 3, 4, 5>, Cont<1, 2, 8, 9, 7, 3>>::type{});
}