在 cppreference add_pointer 中,据说我们可以将其实现为:
namespace detail {
template <class T>
struct type_identity { using type = T; }; // or use std::type_identity (since C++20)
template <class T>
auto try_add_pointer(int) -> type_identity<typename std::remove_reference<T>::type*>;
template <class T>
auto try_add_pointer(...) -> type_identity<T>;
} // namespace detail
template <class T>
struct add_pointer : decltype(detail::try_add_pointer<T>(0)) {};
Run Code Online (Sandbox Code Playgroud)
我的问题是try_add_pointer为了什么?我知道是 SFINAE。但是为什么这里的实现需要它?
如果您阅读 cppreference 上的页面,您会注意到这句话
否则(如果 T 是 cv 或 ref 限定的函数类型),则提供成员 typedef 类型,即类型 T。
函数有自己的类型。通常,您在健全的代码中会看到类似int(int)的类型,即采用单个整数并返回整数的函数类型。这是std::function期望的参数类型,例如std::function<int(int)>。
然而,函数类型集也包含与成员函数相关的奇怪之处。例如
struct foo {
int bar(int) const;
};
Run Code Online (Sandbox Code Playgroud)
int(int) const是 的函数类型bar。虽然这种类型存在于类型系统中,但它的使用受到限制。
[dcl.fct]
6 cv-qualifier-seq 或 ref-qualifier 只能是:
- 非静态成员函数的函数类型,
- 指向成员的指针所指的函数类型,
- 函数 typedef 声明或别名声明的顶级函数类型,
- 类型参数 ([temp.param]) 的默认参数中的类型 ID,或
- 类型参数 ([temp.names]) 的模板参数的类型 ID。
函数声明器中 cv-qualifier-seq 的效果与在函数类型之上添加 cv-qualification 不同。在后一种情况下,忽略 cv 限定符。[ 注意:具有 cv-qualifier-seq 的函数类型不是 cv 限定类型;没有 cv 限定的函数类型。— 尾注 ] [ 示例:
Run Code Online (Sandbox Code Playgroud)typedef void F(); struct S { const F f; // OK: equivalent to: void f(); };— 结束示例 ] 返回类型、参数类型列表、引用限定符和 cv-qualifier-seq,但不是默认参数 ([dcl.fct.default]) 或异常规范 ([except. spec]) 是函数类型的一部分。
因此,该 trait 允许您为它提供一个函数类型,如int() const,并且预计将其返回不变。
这就是try_add_pointer进来的地方。正如您从上面的列表中看到的那样,没有指向此类函数的常规指针,因此我们将在typename std::remove_reference<T>::type*. 但是由于 SFINAE,存在后备。