• C++有许多支持编译期编程的特性
    • C++98前,模板提供了编译期计算的能力,包括使用循环和执行路径选择
    • 使用偏特化,可以在编译期根据特定的约束或要求,在不同的类模板实现之间进行选择
    • 使用SFINAE,允许在不同的函数模板实现之间对不同的类型或不同的约束进行选择
    • 在C11和C14中引入了constexpr特性,编译期计算得到了更好的支持
    • C++17引入了编译期if来禁用依赖于编译期条件或限制的语句,它也可以用于模板之外的场景

模板元编程

  • 模板在编译期实例化,这就启发把模板的一些特性结合到实例化过程中,以此产生一系列递归。下例在编译期找出质数 ```cpp template // p是要检测的数,d是现在的除数 struct DoIsPrime { // 用d到2依次去除p,对所有结果求与 static constexpr bool value = (p % d != 0) && DoIsPrime::value; };

template // 除数减到2为止 struct DoIsPrime { static constexpr bool value = (p % 2 != 0); };

template struct IsPrime { // 除数从p/2开始 static constexpr bool value = DoIsPrime::value; };

// 对于几个特殊用例要单独特化 template<> struct IsPrime<0> { static constexpr bool value = false; }; template<> struct IsPrime<1> { static constexpr bool value = false; }; template<> struct IsPrime<2> { static constexpr bool value = true; }; template<> struct IsPrime<3> { static constexpr bool value = true; };

  1. - 下面是计算`IsPrime<9>::value`的过程
  2. ```cpp
  3. IsPrime<9>::value
  4. // 扩展为
  5. DoIsPrime<9, 4>::value
  6. // 扩展为
  7. 9 % 4 != 0 && DoIsPrime<9, 3>::value
  8. // 扩展为
  9. 9 % 4 != 0 && 9 % 3 != 0 && DoIsPrime<9, 2>::value
  10. // 扩展为
  11. 9 % 4!=0 && 9 % 3 != 0 && 9 % 2 != 0
  12. // 因为9%3为0,结果为false

constexpr函数

  • C++11的constexpr函数仅能由一条return语句组成 ```cpp constexpr bool doIsPrime (unsigned p, unsigned d) { return d != 2 ? (p % d != 0) && doIsPrime(p, d - 1) : (p % 2 != 0); }

constexpr bool isPrime (unsigned p) { return p < 4 ? !(p < 2) : doIsPrime(p, p / 2); // 处理特殊用例 }

  1. - C++14中则不限制只能写一条return语句
  2. ```cpp
  3. constexpr bool isPrime (unsigned p)
  4. {
  5. for (unsigned d = 2; d <= p/2; ++d)
  6. {
  7. if (p % d == 0) return false;
  8. }
  9. return p > 1;
  10. }
  • constexpr函数并不一定在编译期计算。在要求编译期值(如数组长度或非类型模板实参)的上下文中,将在编译期调用constexpr函数,如果不能调用则发出错误。在其他上下文中,不一定会在编译期计算,但如果计算失败则不会发出错误,而是把编译期调用替换为运行期调用
    1. constexpr bool b1 = isPrime(9); // 编译期计算
    2. const bool b2 = isPrime(9); // 同样在编译期计算
    3. // 在块作用域中编译器会决定在编译期还是运行期计算
    4. bool fiftySevenIsPrime()
    5. {
    6. return isPrime(57); // 在编译期或运行期计算
    7. }
    8. // 下面无论x是否为质数都会在运行期计算
    9. int x = 42;
    10. std::cout << isPrime(x); // 运行期计算

使用偏特化选择执行路径

  • 使用偏特化可以在编译期进行不同实现间的选择,比如根据模板实参是否为质数选择不同的实现 ```cpp // 基本辅助模板 template struct Helper;

// 如果SZ不是质数的实现 template struct Helper { … };

// 如果SZ是质数的实现 template struct Helper { … };

template long f(const std::array& coll) { Helper h; // 实现依赖于数组大小是否为质数 … }

  1. - 也可以用基本模板实现一种情况,用偏特化实现另一种情况
  2. ```cpp
  3. // 基本辅助模板(如果没有特化匹配)
  4. template<int SZ, bool = isPrime(SZ)>
  5. struct Helper {
  6. ...
  7. };
  8. // 如果SZ是质数的实现
  9. template<int SZ>
  10. struct Helper<SZ, true> {
  11. ...
  12. };
  • 函数模板不支持偏特化,必须使用其他机制改变函数实现,可选如下

    • 使用带static函数的类
    • 使用std::enable_if
    • 使用SFINAE特性
    • 使用if constexpr(C++17)

      SFINAE

  • 在重载解析的匹配中,为了防止替换过程产生无意义的错误,这样的替换会被忽略,这个原则就是SFINAE (Substitution Failure Is Not An Error) ```cpp // number of elements in a raw array: template std::size_t len(T(&)[N]) { return N; }

// number of elements for a type having size_type: template typename T::size_type len(const T& t) { return t.size(); }

  1. - 第一个函数模板声明参数为`T(&)[N]`,代表参数必须是一个NT类型元素的数组。第二个简单把参数声明为T,但返回类型为`T::size_type`,这就要求传递的实参必须有对应的size_type成员。当传递原始数组或字符串字面值时,只有第一个模板匹配
  2. ```cpp
  3. int a[10];
  4. std::cout << len(a); // OK:只有第一个模板匹配
  5. std::cout << len("tmp"); // OK:只有第一个模板匹配
  • 如果上面的例子要匹配第二个模板,把T替换为int[10]const char[4]也可行,但是返回类型T::size_type导致了隐藏的错误,因此第二个模板会被忽略。同理,传递std::vector时,只有第二个模板匹配

    1. std::vector<int> v;
    2. std::cout << len(v); // OK:只有第二个模板匹配
  • 传递原始指针时,编译器将提示两个模板都不匹配

    1. int* p;
    2. std::cout << len(p); // 错误:找不到匹配的len函数
  • 这和传递一个有size_type成员但没有size()成员函数的对象不同,比如传递std::allocator类型对象时,编译器将匹配第二个函数模板,不会产生匹配错误,而是产生调用size()无效的错误,这次第二个模板不会被忽略

    1. std::allocator<int> x;
    2. std::cout << len(x); // 错误:能找到匹配的len函数,但不能调用size()
  • 当替换返回类型无意义时而忽略一个匹配,会造成编译器选择另一个更差的匹配 ```cpp // number of elements in a raw array: template std::size_t len(T(&)[N]) { return N; }

// number of elements for a type having size_type: template typename T::size_type len(const T& t) { return t.size(); }

// fallback for all other types: std::size_t len(…) // 这个省略号是参数包 { return 0; }

int a[10]; std::cout << len(a); // OK: len() for array is best match std::cout << len(“tmp”); //OK: len() for array is best match std::vector v; std::cout << len(v); // OK: len() for a type with size_type is best match int* p; std::cout << len(p); // OK: only fallback len() matches std::allocator x; std::cout << len(x); // ERROR: 2nd len() function matches best, but can’t call size() for x

  1. - 对于allocator,第二个和第三个函数模板匹配,第二个是更好的匹配,但返回类型没有size()成员会产生错误
  2. - 如果把SFINAE机制用于确保函数模板在某种限制下被忽略,我们就说SFINAE out这个函数,当在C++标准中读到一个函数模板`不应该加入重载解析除非...`,就表示SFINAE被用来SFINAE out这个函数模板,比如[std::thread](https://zh.cppreference.com/w/cpp/thread/thread)声明一个构造函数如下
  3. ```cpp
  4. namespace std {
  5. class thread {
  6. public:
  7. ...
  8. template<typename F, typename... Args>
  9. explicit thread(F&& f, Args&&... args);
  10. ...
  11. };
  12. }
  • 并带有如下remark

    1. Remarks: This constructor shall not participate in overload resolution if decay_t<F> is the same type as std::thread.
  • 这表示这个模板构造函数被调用时,如果std::thread作为第一个和唯一一个实参,则这个模板会被忽略。调用一个线程时,通过SFINAE out这个模板来保证预定义的拷贝或移动构造函数总被使用。标准库提供了禁用模板的工具,最出名的特性是std::enable_if(它是用偏特化和SFINAE实现的)

    1. namespace std {
    2. class thread {
    3. public:
    4. ...
    5. template<typename F, typename... Args,
    6. typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, thread>>>
    7. explicit thread(F&& f, Args&&... args);
    8. ...
    9. };
    10. }
  • 指定正确的表达式来SFINAE out函数模板不会总是那么容易,比如想让函数模板len()对有size_type而没有size()的实参被忽略,之前的方案会产生错误 ```cpp template typename T::size_type len(const T& t) { return t.size(); }

std::allocator x; std::cout << len(x) << ‘\n’; // ERROR: len() selected, but x has no size()

  1. - 对此有一个惯用的解决方法
  2. - 使用尾置返回类型
  3. - 使用decltype和逗号运算符定义返回类型
  4. - 在逗号运算符开始处制定必须有效的全部表达式(转换为void以防逗号表达式被重载)
  5. - 在逗号运算符结尾处定义一个真正返回类型的对象
  6. ```cpp
  7. template<typename T>
  8. auto len(const T& t) -> decltype((void)(t.size()), T::size_type())
  9. {
  10. return t.size();
  11. }

if constexpr

  • C++17引入了编译期if语句,允许基于编译期条件,使用或禁用指定的语句

    1. template<typename T, typename... Types>
    2. void print(const T& firstArg, const Types&... args)
    3. {
    4. std::cout << firstArg << '\n';
    5. if constexpr (sizeof...(args) > 0)
    6. {
    7. print(args...);
    8. }
    9. }
  • 这里如果print()只有一个实参,args为空参数包,sizeof...(args)为0,递归调用print()的语句变为废弃语句,不会实例化,因此不会要求对应的函数存在,递归结束。不会实例化意味着只执行Two-Phase Translation的第一阶段,检查语法正确性和不依赖于模板参数的名称,比如对于下面的例子的检查

    1. template<typename T>
    2. void f(T t)
    3. {
    4. if constexpr (std::is_integral_v<T>)
    5. {
    6. if (t > 0) f(t - 1); // OK
    7. }
    8. else
    9. {
    10. undeclared(t); // error if not declared and not discarded (i.e. T is not integral)
    11. undeclared(); // error if not declared (even if discarded)
    12. static_assert(false, "no integral"); // always asserts (even if discarded)
    13. static_assert(!std::is_integral_v<T>, "no integral"); // OK
    14. }
    15. }
  • if constexpr能用于任何函数而不仅局限于模板,但判断的必须是编译期表达式

    1. int main()
    2. {
    3. if constexpr (std::numeric_limits<char>::is_signed)
    4. {
    5. f(42); // OK
    6. }
    7. else
    8. {
    9. undeclared(42); // error if undeclared() not declared
    10. static_assert(false, "unsigned"); // always asserts(even if discarded)
    11. static_assert(!std::numeric_limits<char>::is_signed, "char is unsigned"); // OK
    12. }
    13. }
  • 把这个特性用于编译期函数isPrimer,如果给定数组大小不是质数则执行附加代码

    1. template<typename T, std::size_t SZ>
    2. void f(const std::array<T, SZ>& coll)
    3. {
    4. if constexpr (!isPrime(SZ)) ...
    5. }