C++模板特化与元函数
2026/6/19 12:46:35 网站建设 项目流程

C++模板特化与元函数

模板特化允许为特定类型提供定制实现,元函数在编译期执行计算。两者结合可以实现强大的编译期类型操作和算法。

完全特化为特定类型提供专门的实现。

#include
#include
#include

template
struct TypeDescriptor {
static std::string name() { return "unknown"; }
static std::string info() { return "generic type"; }
};

template<>
struct TypeDescriptor {
static std::string name() { return "int"; }
static std::string info() { return "integral type, 4 bytes"; }
};

template<>
struct TypeDescriptor {
static std::string name() { return "double"; }
static std::string info() { return "floating point, 8 bytes"; }
};

template<>
struct 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";
}

偏特化为模板参数子集提供特殊实现。

template
struct Pair {
T first;
U second;
void print() { std::cout << "generic pair\n"; }
};

template
struct Pair {
T first;
T second;
void print() { std::cout << "same-type pair\n"; }
T sum() { return first + second; }
};

template
struct 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();
}

元函数在编译期执行计算。

template
struct Factorial {
static constexpr int value = N * Factorial::value;
};

template<>
struct Factorial<0> {
static constexpr int value = 1;
};

template
struct Fibonacci {
static constexpr int value = Fibonacci::value + Fibonacci::value;
};

template<>
struct Fibonacci<0> { static constexpr int value = 0; };

template<>
struct Fibonacci<1> { static constexpr int value = 1; };

void meta_functions() {
static_assert(Factorial<5>::value == 120);
static_assert(Fibonacci<10>::value == 55);

std::cout << "5! = " << Factorial<5>::value << "\n";
std::cout << "Fib(10) = " << Fibonacci<10>::value << "\n";
}

条件类型元函数。

template
struct IfThenElse {
using type = TrueType;
};

template
struct 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";
}

类型特征元函数。

template
struct IsPointer {
static constexpr bool value = false;
};

template
struct IsPointer {
static constexpr bool value = true;
};

template
struct RemoveConst {
using type = T;
};

template
struct 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";
}

编译期整数序列。

template
struct IntegerSequence {
static constexpr int size = sizeof...(Is);
};

template
struct MakeIntegerSequence : MakeIntegerSequence {};

template
struct MakeIntegerSequence<0, Is...> {
using type = IntegerSequence;
};

void integer_sequence() {
using Seq = MakeIntegerSequence<5>::type;
static_assert(Seq::size == 5);
std::cout << "IntegerSequence size: " << Seq::size << "\n";
}

类型列表元函数。

template
struct TypeList {
static constexpr size_t size = sizeof...(Ts);
};

template
struct Length;

template
struct Length> {
static constexpr size_t value = sizeof...(Ts);
};

template
struct TypeAt;

template
struct TypeAt<0, TypeList> {
using type = Head;
};

void typelist_meta() {
using MyTypes = TypeList;
static_assert(Length::value == 3);

using FirstType = TypeAt<0, MyTypes>::type;
static_assert(std::is_same::value);

std::cout << "TypeList metafunctions work\n";
}

模板特化和元函数是C++泛型编程和编译期计算的基础工具。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询