目录
[3.if constexpr](#3.if constexpr)
一.模板元编程
1.背景
现代C++的一个进化方向就是在编译时做更多的工作,模板元编程是C++中一种利用模板机制在编译期进行计算和代码生成的高级技术。它通过模板特化、递归实例化和类型操作,在编译时完成传统运行时才能处理的任务,从而实现零运行时开销的优化。
模板元编程最早由Erwin Unruh在1994年发现,他展示了如何让编译器在错误信息中输出素数序列,随后被Todd Veldhuizen和David Vandevoorde等人系统化,Todd Veldhuizen证明了C++模板具有图灵完备性,理论上能执行任何计算任务,它遵循函数式编程范式,模板参数作为不可变数据参与编译期计算。
2.核心概念
模板元编程的本质是将计算从运行时转移到编译期,利用编译器作为计算引擎生成高效代码。其核心思想包括:
a.编译期计算:所有运算在编译阶段完成,结果直接嵌入最终程序
b.类型操作:通过模板参数推导和类型萃取操作类型
c.递归模板实例化:通过递归展开实现循环和条件逻辑
d.零运行时开销:结果在编译期确定,不增加程序运行负担
3.基础语法
a.基本模板结构
模板元编程主要使用类模板,而非函数模板,因为类模板可以包含类型成员和静态成员,再利用模板特化和递归实现。
cpp
template<typename T>
struct MyTemplate
{
using type = T; // 类型成员
static const int value = 42; // 静态成员
};
b.编译期值计算
最简单的模板元编程是编译期计算阶乘,代码如下所示:
cpp
template<unsigned int N>
struct Factorial
{
static const unsigned int value = N * Factorial<N - 1>::value;
};
// 终⽌条件特化
template <>
struct Factorial<0>
{
static const unsigned int value = 1;
};
int main()
{
constexpr unsigned int fact5 = Factorial<5>::value; // 编译时计算出120
return 0;
}
c.编译期类型计算
编译时获取或修改类型信息的操作,下面代码展示了元编程在编译期进行类型推导、计算、断言,而不产生任何运行期开销。
cpp
#include<iostream>
using namespace std;
namespace temp
{
// 主模板
template<typename T>
struct is_pointer {
static constexpr bool value = false;
};
// 针对指针类型的偏特化
template<typename T>
struct is_pointer<T*> {
static constexpr bool value = true;
};
// 主模板,默认情况类型不同
template<typename T, typename U>
struct is_same {
static constexpr bool value = false;
};
// 特化版本,当两个类型相同时
template<typename T>
struct is_same<T, T> {
static constexpr bool value = true;
};
// 移除 const
// 主模板,默认情况下不改变类型
template <typename T>
struct remove_const {
using type = T;
};
// 针对 const T 的特化版本,移除 const
template <typename T>
struct remove_const<const T> {
using type = T;
};
// 移除 指针
template <typename T>
struct remove_pointer {
using type = T;
};
template <typename T>
struct remove_pointer<T*> {
using type = T;
};
template <typename T>
struct remove_pointer<T* const> {
using type = T;
};
void func()
{
static_assert(is_pointer<int*>::value, "int* is a pointer");
// static_assert(bit::is_pointer<int>::value, "int is not a pointer");
static_assert(is_same<int, int>::value, "int and int should be the
same");
// static_assert(is_same<int, float>::value, "int and float should be
different");
static_assert(is_same<remove_pointer<int*>::type, int>::value, "int
and int should be the same");
static_assert(is_same<remove_const<const int>::type, int>::value, "int
and int should be the same");
}
}
核心机制:模板特化
元编程的判断和选择能力,几乎全部依赖于模板特化。编译器就像一个极度严谨的匹配引擎。主模板提供最泛化、默认的兜底方案。偏特化针对特定模式提供更精确的匹配。当有更精确的匹配时,编译器会优先选择特化版本。
cpp
template<typename T>
struct is_pointer
{
static constexpr bool value = false;
}; // 主模板
template<typename T>
struct is_pointer<T*>
{
static constexpr bool value = true;
}; // 偏特化
is_pointer用于判断是否为指针,例如当传入int*时,匹配偏特化版本,T为int,value为true。当传入int时,匹配不上T*,只能退回主模板,value为false。static constexpr bool value,说明value是一个编译期常量,不占用对象内存,可以直接用于static_assert或模板参数。
cpp
template<typename T, typename U>
struct is_same
{
static constexpr bool value = false;
}; // 主模板
template<typename T>
struct is_same<T, T>
{
static constexpr bool value = true;
}; // 偏特化
is_same用于判断两个类型是否相同,原理为主模板接收两个独立类型T和U,特化版本写成了is_same<T,T>。这意味着只有当传入的两个类型完全一致时,特化版本才会被匹配。如果传入is_same<int,float>,编译器无法推导出匹配T,只能退回主模板,返回false,从而实现判断类型是否相同。
cpp
template<typename T>
struct remove_const
{
using type = T;
}; // 主模板
template<typename T>
struct remove_const<const T>
{
using type = T;
}; // 偏特化
remove_const用于移除const属性,当传入const int时,匹配特化版本,T为int,type被定义为int。传入int时,退回主模板,type就是int。
这里没有用value,而是用了using type=T。这种通过::type提取类型的方法,就是模板元编程最常用的类型萃取方法。
cpp
template<typename T>
struct remove_pointer
{
using type = T;
};
template<typename T>
struct remove_pointer<T*>
{
using type = T;
};
template<typename T>
struct remove_pointer<T* const>
{
using type = T;
};
remove_pointer用于移除指针属性,针对int*,匹配T*,type变成int,针对int* const,匹配T* const,type推导为int。这展现了偏特化处理复杂类型模式的能力。
cpp
static_assert(is_same<remove_pointer<int*>::type, int>::value, "...");
以上代码是一句纯正的编译期元编程:执行逻辑为先调用remove_pointer<int*>,提取出内部的type(即int)。将提取出的int和另一个int传给is_same。is_same匹配到特定版本,算出value=true。static_assert在编译期验证true。如果断言失败,则代码无法生成可执行文件。
d.类型萃取
类型萃取是C++模板元编程中的核心技术,它允许在编译时检查和修改类型特性。C++11版本标准库在<type_traits>头文件中提供了大量类型萃取工具。类型萃取是通过模板特化技术实现的编译期类型操作,主要用途包括:检查类型特性、修改/转换类型、根据类型特性进行编译期分支。
标准库常见的类型萃取:
cpp
#include <type_traits>
// 1、基础类型检查
std::is_void<void>::value; // true
std::is_integral<int>::value; // true
std::is_floating_point<float>::value; // true
std::is_pointer<int*>::value; // true
std::is_reference<int&>::value; // true
std::is_const<const int>::value; // true
// 2、复合类型检查
std::is_function<void()>::value; // true
std::is_member_object_pointer<int (Foo::*)>::value; // true
std::is_compound<std::string>::value; // true (⾮基础类型)
// 3、类型关系检查
std::is_same<int, int32_t>::value; // 取决于平台
std::is_base_of<Base, Derived>::value;
std::is_convertible<From, To>::value;
// 4、类型修改
std::add_const<int>::type; // const int
std::add_pointer<int>::type; // int*
std::add_lvalue_reference<int>::type; // int&
std::remove_const<const int>::type; // int
std::remove_pointer<int*>::type; // int
std::remove_reference<int&>::type; // int
// 4、条件类型选择
std::conditional<true, int, float>::type; // int
std::conditional<false, int, float>::type; // float
// 5、类型推导
// 函数的返回结果类型
std::result_of<F(Args...)>::type; // C++17以后被废弃
std::invoke_result<F, Args...>::type; // C++17以后使⽤这个
template<class F, class... Args>
using invoke_result_t = typename invoke_result<F, Args...>::type;
C++17为类型萃取添加了_v和_t后缀的便利变量模板和类型别名
cpp
// C++11⽅式
std::is_integral<int>::value;
std::remove_const<const int>::type;
// C++14、C++17 更简洁的⽅式
std::is_integral_v<int>;
std::remove_const_t<const int>;
// C++17 引⼊的辅助变量模板
template<typename T>
inline constexpr bool is_integral_v = is_integral<T>::value;
// C++14 引⼊的辅助别名模板
template<typename T>
using remove_const_t = typename remove_const<T>::type;
类型萃取库的一些使用样例展示:
cpp
#include<iostream>
//using namespace std;
template<typename T>
void process(T value)
{
if constexpr (std::is_pointer_v<T>)
{
// 指针类型的处理
std::cout << "Processing pointer: " << *value << std::endl;
}
else if constexpr (std::is_integral_v<T>)
{
// 整数类型的处理
std::cout << "Processing integer: " << value * 2 << std::endl;
}
else if constexpr (std::is_floating_point_v<T>)
{
// 浮点类型的处理
std::cout << "Processing float: " << value / 2.0 << std::endl;
}
else
{
// 默认处理
std::cout << "Processing unknown type" << std::endl;
}
}
#if defined(_WIN32)
#include<winsock2.h>
using socket_t = SOCKET;
#else
using socket_t = int;
#endif
template<typename T>
void close_handle(T handle)
{
if constexpr (std::is_same_v<T, SOCKET>)
{
closesocket(handle);
}
else
{
close(handle);
}
}
int main()
{
// 使⽤
int i = 42;
process(i); // Processing integer: 84
process(&i); // Processing pointer: 42
process(3.14); // Processing float: 1.57
process("hello"); // Processing unknown type
return 0;
}
类型萃取在STL库中的一些使用剖析:
cpp
// 类型萃取在stl中⼀些原理分析及应⽤
#pragma once
#include<assert.h>
namespace bit
{
/*struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag {};
struct random_access_iterator_tag : public bidirectional_iterator_tag {};*/
template <class Iterator>
struct iterator_traits {
typedef typename Iterator::iterator_category iterator_category;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::difference_type difference_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
};
template <class T>
struct iterator_traits<T*>
{
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
};
template <class InputIterator>
inline iterator_traits<InputIterator>::difference_type
__distance(InputIterator first, InputIterator last,
input_iterator_tag)
{
typename iterator_traits<InputIterator>::difference_type n = 0;
while (first != last) {
++first; ++n;
}
return n;
}
template <class RandomAccessIterator>
inline iterator_traits<RandomAccessIterator>::difference_type
__distance(RandomAccessIterator first, RandomAccessIterator last,
random_access_iterator_tag) {
return last - first;
}
template<class InputIterator>
inline typename iterator_traits<InputIterator>::difference_type
distance(InputIterator first, InputIterator last)
{
typedef typename iterator_traits<InputIterator>::iterator_category
category;
return __distance(first, last, category());
}
template<class T>
class vector
{
public:
typedef T* iterator;
// 类模板的成员函数,也可以是⼀个函数模板
template <class InputIterator>
vector(InputIterator first, InputIterator last)
{
// 为了提⾼效率可以提前resize
// 这⾥InputIterator是模板参数,随机迭代器⽀持减计算个数
// 单向和双向迭代器只是迭代计数计算个数
// resize(last - first);
resize(bit::distance(first, last));
while (first != last)
{
// push_back(*first);
++first;
}
}
void resize(size_t n) {}
private:
iterator _start = nullptr;
iterator _finish = nullptr;
iterator _end_of_storage = nullptr;
};
}
int main()
{
string s("hello world");
bit::vector<char> v1(s.begin(), s.end());
std::list<int> lt(10000, 1);
bit::vector<int> v2(lt.begin(), lt.end());
return 0;
}
e.SFINAE
SFINAE是Substitution Failure Is Not An Error首字母缩写,意思为替换失败不是错误,在模板参数推导或替换时,如果某个候选模板导致编译错误(如类型不匹配、无效表达式等),编译不会直接报错,而是跳过该候选,尝试其他可行的版本。如果最后都没匹配到合适的版本,再进行报错。
SFINAE的语法相对复杂难理解,在C++20以后,考虑使用概念替代绝大部分的SFINAE。
SFINAE经典应用场景函数重载
cpp
// 版本1:仅适⽤于可递增的类型(如 int)
template<typename T>
auto foo(T x) -> decltype(++x, void())
{
std::cout << "foo(T): " << x << " (can be incremented)\n";
}
// C++17 使⽤void_t优化上⾯的写法
//template<typename T>
//auto foo(T x) -> std::void_t<decltype(++x)>
{
// std::cout << "foo(T): " << x << " (can be incremented)\n";
//}
// 版本2:回退版本
void foo(...)
{
std::cout << "foo(...): fallback (cannot increment)\n";
}
int main()
{
foo(42); // 调⽤版本1(int ⽀持 ++x)
foo(std::string("1111")); // 调⽤版本2(string 不⽀持 ++x)
}
std::enable_if是一个类型萃取,它是SFINAE的典型应用,用于在编译时启用/禁用函数模板。
cpp
#include<type_traits>
#include<iostream>
// 对于整数类型启⽤此重载
template<typename T>
typename std::enable_if_t<std::is_integral_v<T>, T>
add_one(T t)
{
return t + 1;
}
// 对于浮点类型启⽤此重载
template<typename T>
typename std::enable_if_t<std::is_floating_point_v<T>, T>
add_one(T t)
{
return t + 2.0;
}
// 模板参数的检查
template<typename T,
typename = std::enable_if_t<std::is_integral_v<T>>>
void process_integer(T value)
{
// 只接受整数类型
}
int main()
{
std::cout << add_one(5) << "\n"; // 调⽤整数版本,输出6
std::cout << add_one(3.14) << "\n"; // 调⽤浮点版本,输出4.14
// add_one("hello"); // 编译错误,没有匹配的重载
process_integer(1);
// process_integer(1.1); // 编译错误,没有匹配的重载
}
二、模板元编程特性优化
1.constexpr
C++11/14/17/20逐步增强了constexpr能力,许多模板元编程任务可以用constexpr函数替代,允许函数和变量在编译期求值,替代部分传统模板元编程的递归实例化,constexpr简化了很多相对复杂的模板元编程实现。
cpp
constexpr int factorial(int n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
constexpr int x = factorial(5); // 120
2.变量模板
变量模板直接定义编译期常量值,有了变量模板类型萃取的一些取值特性就可以简化一些,如is_integral_v等。
cpp
template<typename T>
constexpr T pi = T(3.1415926535897932385);
template< class T >
constexpr bool is_integral_v = is_integral<T>::value;
int main()
{
float f = pi<float>; // 单精度 π
double x = pi<double>; // 双精度 π
// 使⽤不同精度的 π
std::cout.precision(6);
std::cout << "float π: " << f << std::endl;
std::cout.precision(10);
std::cout << "double π: " << x << std::endl;
return 0;
}
3.if constexpr
if constexpr可在编译期根据条件选择代码路径,避免生成无效代码分支,简化SFINAE和模板特化的复杂逻辑,提升可读性。
cpp
template <typename T>
auto process(T value)
{
if constexpr (std::is_integral_v<T>)
{
return value * 2;
}
else if constexpr (std::is_floating_point_v<T>)
{
return value / 2;
}
else
{
return value;
}
}
4.折叠表达式
折叠表达式用于简化可变参数模板的参数包展开操作:
cpp
template<typename... Args>
void print(Args&&... args)
{
(std::cout << ... << args) << '\n';
// 等价于 (((std::cout << arg1) << arg2) << ...) << argN;
}
5.概念
概念是C++20引入的模板参数约束机制,用于取代SFINAE的复杂约束语法。
cpp
// 定义⼀个要求T是整形的概念
template< class T >
concept Integral = std::is_integral_v<T>;
// 1、模板参数后直接使⽤
template<Integral T>
void f1(T x)
{
std::cout << "有 concepts 约束" << std::endl;
}
6.模块
C++20引入的模块是C++语言的一项重大革新,旨在解决传统头文件包含机制的诸多问题。其中一个问题就是,每次包含头文件时,编译器都需要重新解析其内容,导致编译时间大幅增加。模块引入以后可以大大缩短编译时间。模块元编程的一个重大问题就是让项目的编译时间变长,所以模块的引入可以很好的缓解这个问题。

三、模板元编程优缺点
优点:
1.零运行时开销:所有计算在编译期完成
2.类型安全:编译期类型检查
3.高度抽象:可构建灵活通用的库
缺点:
1.编译时间长:复杂的模板实例化会增加编译时间
2.学习成本增加:很多模板元编程的写法晦涩难懂
3.错误信息晦涩:模板错误通常难以理解
4.调试困难:难以调试编译期计算
四、结语
C++模板元编程的演进,本质上是一部追求极致性能与泛型抽象的进化史。从早期依赖模板特化与SFINAE进行艰难的编译期推导,到现代C++引入constexpr与if constexpr实现优雅的编译期分支剪裁,其核心逻辑始终如一:将计算与检查前置到编译期,在运行期实现真正的零开销抽象。尽管它不可避免地带来了编译时间增长与代码可读性下降等工程挑战,但正是这项技术构筑了现代C++高性能基础库的坚实底座。深入掌握模板元编程,不仅是跨越C++高阶开发门槛的必经之路,更是构建高维泛型架构的基石。