
C模板特化与元函数模板特化允许为特定类型提供定制实现元函数在编译期执行计算。两者结合可以实现强大的编译期类型操作和算法。完全特化为特定类型提供专门的实现。#include#include#includetemplatestruct TypeDescriptor {static std::string name() { return unknown; }static std::string info() { return generic type; }};templatestruct TypeDescriptor {static std::string name() { return int; }static std::string info() { return integral type, 4 bytes; }};templatestruct TypeDescriptor {static std::string name() { return double; }static std::string info() { return floating point, 8 bytes; }};templatestruct TypeDescriptor {static std::string name() { return std::string; }static std::string info() { return variable length string; }};void full_specialization() {std::cout TypeDescriptor::name() : TypeDescriptor::info() \n;std::cout TypeDescriptor::name() : TypeDescriptor::info() \n;std::cout TypeDescriptor::name() : TypeDescriptor::info() \n;}偏特化为模板参数子集提供特殊实现。templatestruct Pair {T first;U second;void print() { std::cout generic pair\n; }};templatestruct Pair {T first;T second;void print() { std::cout same-type pair\n; }T sum() { return first second; }};templatestruct Pair {T first;T* second;void print() { std::cout pointer pair\n; }};void partial_specialization() {Pair p1;p1.print();Pair p2;p2.print();std::cout Sum: p2.sum() \n;Pair p3;p3.print();}元函数在编译期执行计算。templatestruct Factorial {static constexpr int value N * Factorial::value;};templatestruct Factorial0 {static constexpr int value 1;};templatestruct Fibonacci {static constexpr int value Fibonacci::value Fibonacci::value;};templatestruct Fibonacci0 { static constexpr int value 0; };templatestruct Fibonacci1 { static constexpr int value 1; };void meta_functions() {static_assert(Factorial5::value 120);static_assert(Fibonacci10::value 55);std::cout 5! Factorial5::value \n;std::cout Fib(10) Fibonacci10::value \n;}条件类型元函数。templatestruct IfThenElse {using type TrueType;};templatestruct IfThenElse {using type FalseType;};void conditional_type() {using IntType IfThenElse(sizeof(int) 2), int, short::type;using DoubleType IfThenElse(sizeof(double) 4), double, float::type;static_assert(std::is_same::value);static_assert(std::is_same::value);std::cout Conditional types work\n;}类型特征元函数。templatestruct IsPointer {static constexpr bool value false;};templatestruct IsPointer {static constexpr bool value true;};templatestruct RemoveConst {using type T;};templatestruct RemoveConst {using type T;};void type_traits_meta() {static_assert(IsPointer::value);static_assert(!IsPointer::value);using type RemoveConst::type;static_assert(std::is_same::value);std::cout Type traits meta-functions work\n;}编译期整数序列。templatestruct IntegerSequence {static constexpr int size sizeof...(Is);};templatestruct MakeIntegerSequence : MakeIntegerSequence {};templatestruct MakeIntegerSequence0, Is... {using type IntegerSequence;};void integer_sequence() {using Seq MakeIntegerSequence5::type;static_assert(Seq::size 5);std::cout IntegerSequence size: Seq::size \n;}类型列表元函数。templatestruct TypeList {static constexpr size_t size sizeof...(Ts);};templatestruct Length;templatestruct Length {static constexpr size_t value sizeof...(Ts);};templatestruct TypeAt;templatestruct TypeAt0, TypeList {using type Head;};void typelist_meta() {using MyTypes TypeList;static_assert(Length::value 3);using FirstType TypeAt0, MyTypes::type;static_assert(std::is_same::value);std::cout TypeList metafunctions work\n;}模板特化和元函数是C泛型编程和编译期计算的基础工具。