C++ 模板:编写泛型库需要的基本技术

  • Callables
    • std::invoke()
    • 函数对象 Function Objects
    • 处理成员函数及额外的参数
    • 统一包装
  • 泛型库的其他基本技术
    • Type Traits
    • std::addressof()
    • std::declval
  • 完美转发 Perfect Forwarding
  • 作为模板参数的引用
  • 延迟计算 Defer Evaluations

Callables

许多基础库都要求调用方传递一个可调用的实体(entity)。例如:一个描述如何排序的函数、一个如何 hash 的函数。一般用 callback
来描述这种用法。在 C++中有以下几种形式可以实现 callback,它们都可以被当做函数参数传递并可以直接使用类似 f(...)
的方式调用:

  • 指向函数的指针。
  • 重载了 operator()
    的类(有时被叫做 functors
    ),包括 lambdas.
  • 包含一个可以生成函数指针或者函数引用的转换函数的类。

C++使用 callable type
来描述上面这些类型。比如,一个可以被调用的对象称作 callable object
,我们使用 callback
来简化这个称呼。
编写泛型代码会因为这个用法的存在而可扩展很多。

函数对象 Function Objects

例如一个 for_each 的实现:

template <typename Iter, typename Callable>
void foreach (Iter current, Iter end, Callable op) {
while (current != end) { // as long as not reached the end
op(*current); // call passed operator for current element
++current; // and move iterator to next element
}
}

使用不同的 Function Objects
来调用这个模板:

// a function to call:
void func(int i) { std::cout << "func() called for: " << i << '\n'; }

// a function object type (for objects that can be used as functions):
class FuncObj {
public:
void operator()(int i) const { // Note: const member function
std::cout << "FuncObj::op() called for: " << i << '\n';
}
};


int main(int argc, const char **argv) {
std::vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19};

foreach (primes.begin(), primes.end(), func); // range function as callable (decays to pointer)
foreach (primes.begin(), primes.end(), &func); // range function pointer as callable

foreach (primes.begin(), primes.end(), FuncObj()); // range function object as callable

foreach (primes.begin(), primes.end(), // range lambda as callable
[](int i "") {
std::cout << "lambda called for: " << i << '\n';
});
return 0;
}

解释一下:

  • foreach (primes.begin(), primes.end(), func);
    按照值传递时,传递函数会 decay 为一个函数指针。
  • foreach (primes.begin(), primes.end(), &func);
    这个比较直接,直接传递了一个函数指针。
  • foreach (primes.begin(), primes.end(), FuncObj());
    functor
    operator()
    op(*current);
    op.operator()(*current);
    
  • Lambda :这个和前面情况一样,不解释了。

处理成员函数及额外的参数

上面没有提到一个场景 :成员函数。因为调用非静态成员函数的方式是 object.memfunc(. . . )
ptr->memfunc(. . . )
,不是统一的 function-object(. . . )

std::invoke()

幸运的是,从 C++17 起,C++提供了 `std::invoke()`
[1]
来统一所有的 callback 形式:

template <typename Iter, typename Callable, typename... Args>
void foreach (Iter current, Iter end, Callable op, Args const &... args) {
while (current != end) { // as long as not reached the end of the elements
std::invoke(op, // call passed callable with
args..., // any additional args
*current); // and the current element
++current;
}
}

那么, std::invoke()
是怎么统一所有 callback 形式的呢? 注意,我们在 foreach 中添加了第三个参数: Args const &... args
. invoke 是这么处理的:

  • **如果 Callable 是指向成员函数的指针,**它会使用 args 的第一个参数作为类的 this。args 中剩余的参数被传递给 Callable。
  • 否则,所有 args 被传递给 Callable。

使用:

// a class with a member function that shall be called
class MyClass {
public:
void memfunc(int i) const {
std::cout << "MyClass::memfunc() called for: " << i << '\n';
}
};

int main() {
std::vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19};

// pass lambda as callable and an additional argument:
foreach (
primes.begin(), primes.end(), // elements for 2nd arg of lambda
[](std::string const &prefix, int i "") { // lambda to call
std::cout << prefix << i << '\n';
},
"- value: "); // 1st arg of lambda

// call obj.memfunc() for/with each elements in primes passed as argument
MyClass obj;
foreach (primes.begin(), primes.end(), // elements used as args
&MyClass::memfunc, // member function to call
obj); // object to call memfunc() for
}

注意在 callback 是成员函数的情况下,是如何调用 foreach 的。

统一包装

std::invoke()
的一个场景用法是:包装一个函数调用,这个函数可以用来记录函数调用日志、测量时间等。

#include                // for std::invoke()
#include // for std::forward()

template<typename Callable, typename... Args>
decltype(auto) call(Callable&& op, Args&&... args)
{
return std::invoke(std::forward(op), std::forward(args)...); // passed callable with any additional args
}

一个需要考虑的事情是,如何处理 op 的返回值并返回给调用者:

template<typename Callable, typename... Args>
decltype(auto) call(Callable&& op, Args&&... args)

这里使用 decltype(auto)
(从 C++14 起)( decltype(auto)
的用法可以看之前的文章 : c++11-17 模板核心知识(九)—— 理解 decltype 与 decltype(auto))

如果想对返回值做处理,可以声明返回值为 decltype(auto)

decltype(auto) ret{std::invoke(std::forward(op), std::forward(args)...)};

...
return ret;

但是有个问题,使用 decltype(auto)
声明变量,值不允许为 void,可以针对 void 和非 void 分别进行处理:

#include   // for std::forward()
#include // for std::is_same and invoke_result
#include // for std::invoke()

template <typename Callable, typename... Args>
decltype(auto) call(Callable &&op, Args &&... args) {

if constexpr (std::is_same_v<std::invoke_result_t, void>) {
// return type is void:
std::invoke(std::forward(op), std::forward(args)...);
...
return;
} else {
// return type is not void:
decltype(auto) ret{
std::invoke(std::forward(op), std::forward(args)...)};
...
return ret;
}
}

std::invoke_result
只有从 C++17 起才能使用,C++17 之前只能用 typename std::result_of::type
.

泛型库的其他基本技术

Type Traits

这个技术很多人应该很熟悉,这里不细说了。

#include 

template <typename T>
class C {

// ensure that T is not void (ignoring const or volatile):
static_assert(!std::is_same_v<std::remove_cv_t, void>,
"invalid instantiation of class C for void type");

public:
template <typename V> void f(V &&v) {
if constexpr (std::is_reference_v) {
... // special code if T is a reference type
}
if constexpr (std::is_convertible_v<std::decay_t, T>) {
... // special code if V is convertible to T
}
if constexpr (std::has_virtual_destructor_v) {
... // special code if V has virtual destructor
}
}
};

这里,我们使用 type_traits 来进行不同的实现。

std::addressof()

可以使用 std::addressof()
获取对象或者函数 真实的地址
, 即使它重载了 operator &
. 不过这种情况不是很常见。当你想获取任意类型的真实地址时,推荐使用 std::addressof():

template<typename T>
void f (T&& x)
{
auto p = &x; // might fail with overloaded operator &
auto q = std::addressof(x); // works even with overloaded operator &
...
}

比如在 STL vector 中,当 vector 需要扩容时,迁移新旧 vector 元素的代码:

{
for (; __first != __last; ++__first, (void)++__cur) std::_Construct(std::__addressof(*__cur), *__first);
return __cur;
}

template <typename _T1, typename... _Args>
inline void _Construct(_T1 *__p, _Args &&... __args) {
::new (static_cast<void *>(__p)) _T1(std::forward(__args)...); //实际copy(或者move)元素
}

这里使用 std::addressof()
获取新 vector 当前元素的地址,然后进行 copy(或 move)。可以看之前写的
c++ 从 vector 扩容看 noexcept 应用场景

std::declval

std::declval
[2]
可以被视为某一特定类型对象引用的占位符。它不会创建对象,常常和 decltype 和 sizeof 搭配使用。因此,在不创建对象的情况下,可以假设有相应类型的可用对象,即使该类型没有默认构造函数或该类型不可以创建对象。

注意,declval 只能在 unevaluated contexts
[3]
中使用。
一个简单的例子:

class Foo;     //forward declaration
Foo f(int); //ok. Foo is still incomplete
using f_result = decltype(f(11)); //f_result is Foo

现在如果我想获取使用 int 调用 f()后返回的类型是什么?是 decltype(f(11))
?看起来怪怪的,使用 declval 看起来就很明了:

decltype(f(std::declval<int>()))

还有就是之前
c++11-17 模板核心知识(一)—— 函数模板

中的例子)——返回多个模板参数的公共类型:

template <typename T1, typename T2,
typename RT = std::decay_t<decltype(true ? std::declval()
: std::declval())>>
RT max(T1 a, T2 b) {
return b < a ? a : b;
}

这里在为了避免在 ?:
中不得不去调用 T1 和 T2 的构造函数去创建对象,我们使用 declval 来避免创建对象,而且还可以达到目的。ps. 别忘了使用 std::decay_t,因为 declval 返回的是一个 rvalue references. 如果不用的话, max(1,2)
会返回 int&&
.
最后看下官网的例子:

#include 
#include

struct Default { int foo() const { return 1; } };

struct NonDefault
{

NonDefault() = delete;
int foo() const { return 1; }
};

int main()
{
decltype(Default().foo()) n1 = 1; // type of n1 is int
// decltype(NonDefault().foo()) n2 = n1; // error: no default constructor
decltype(std::declval().foo()) n2 = n1; // type of n2 is int
std::cout << "n1 = " << n1 << '\n'
<< "n2 = " << n2 << '\n';
}

完美转发 Perfect Forwarding

template<typename T>
void f (T&& t) // t is forwarding reference {
g(std::forward(t))
; // perfectly forward passed argument t to g()
}

或者转发临时变量,避免无关的拷贝开销:

template<typename T>
void foo(T x)
{
auto&& val = get(x);
...

// perfectly forward the return value of get() to set():
set(std::forward<decltype(val)>(val));
}

作为模板参数的引用

template<typename T>
void tmplParamIsReference(T)
{
std::cout << "T is reference: " << std::is_reference_v << '\n';
}

int main() {
std::cout << std::boolalpha;
int i;
int& r = i;
tmplParamIsReference(i); // false
tmplParamIsReference(r); // false
tmplParamIsReference<int&>(i); // true
tmplParamIsReference<int&>(r); // true
}

这点也不太常见,在前面的文章 c++11-17 模板核心知识(七)—— 模板参数 按值传递 vs 按引用传递提到过一次。这个会改变强制改变模板的行为,即使模板的设计者一开始不想这么设计。
我没怎么见过这种用法,而且这种用法有的时候会有坑,大家了解一下就行。
可以使用 static_assert 禁止这种用法:

template<typename T>
class optional {
static_assert(!std::is_reference::value, "Invalid instantiation of optional for references");

};

延迟计算 Defer Evaluations

首先引入一个概念:incomplete types. 类型可以是 complete 或者 incomplete,incomplete types 包含:

  • 类只声明没有定义。
  • 数组没有定义大小。
  • 数组包含 incomplete types。
  • void
  • 枚举类型的 underlying type 或者枚举类型的值没有定义。

可以理解 incomplete types 为只是定义了一个标识符但是没有定义大小。例如:

class C;     // C is an incomplete type
C const* cp; // cp is a pointer to an incomplete type
extern C elems[10]; // elems has an incomplete type
extern int arr[]; // arr has an incomplete type
...
class C { }; // C now is a complete type (and therefore cpand elems no longer refer to an incomplete type)
int arr[10]; // arr now has a complete type

现在回到 Defer Evaluations 的主题上。考虑如下类模板:

template<typename T>
class Cont {
private:
T* elems;
public:
...
};

现在这个类可以使用 incomplete type,这在某些场景下很重要,例如链表节点的简单实现:

struct Node {
std::string value;
Cont next; // only possible if Cont accepts incomplete types
};

但是,一旦使用一些 type_traits,类就不再接受 incomplete type:

template <typename T>
class Cont {
private:
T *elems;

public:
...

typename std::conditional<std::is_move_constructible::value, T &&, T &>::type
foo()
;
};

std::conditional
也是一个 type_traits,这里的意思是:根据 T 是否支持移动语义,来决定 foo()返回 T &&
还是 T &
.

但是问题在于, std::is_move_constructible
需要它的参数是一个 complete type. 所以,之前的 struct Node 这种声明会失败(不是所有的编译器都会失败。其实这里我理解不应该报错,因为按照类模板实例化的规则,成员函数只有用到的时候才进行实例化)。
我们可以使用 Defer Evaluations 来解决这个问题:

template <typename T>
class Cont {
private:
T *elems;

public:
...

template<typename D = T>
typename std::conditional<std::is_move_constructible::value, T &&, T &>::type
foo();
};

这样,编译器就会直到 foo()被 complete type 的 Node 调用时才实例化。
(完)

参考资料

[1]

std::invoke()
: https://en.cppreference.com/w/cpp/utility/functional/invoke

[2]

std::declval
: https://en.cppreference.com/w/cpp/utility/declval

[3]

unevaluated contexts: https://en.cppreference.com/w/cpp/language/expressions#Unevaluated_expressions