理解UE4中C++17的...符号及enable_if_t的用法及SFINAE思想

下面是一段C++17的代码:

//函数1:

template <typename... BufferTypes,

std::enable_if_t<std::conjunction<CanAppendBufferType<std::decay_t<BufferTypes>>...>::value> * = nullptr>

inline explicit FCompositeBuffer(BufferTypes&&... Buffers)

{

if constexpr (sizeof...(Buffers) > 0)

{

Segments.Reserve((GetBufferCount(Forward<BufferTypes>(Buffers)) + ...));

(AppendBuffers(Forward<BufferTypes>(Buffers)), ...);

Segments.RemoveAll(&FSharedBuffer::IsNull);

}

}

红色字体表示语法上的整体。

1、 template <typename... BufferTypes, 表示声明若干个类型名;

2、CanAppendBufferType< std::decay_t<BufferTypes> > 这是一个关于类型的调用,具体含义先忽略;

3、std::conjunction<CanAppendBufferType<std::decay_t<BufferTypes>>...>

将它看成此形式: std::conjunction< condition<BufferTypes>... >

此时,红色字体是一个整体,... 将会unpack(解压) condition<SomeArgs> 这个模式,并以逗号分割的参数形式,传递给 std::conjunction ,即等价于:

std::conjunction< condition<BT1>, condition<BT2>, condition<BT3> >

4、std::conjunction< a, b, c >::value 的意思是 a || b || c 的值,但是,它是模板元编程,也就是在"编译时期"计算 a||b||c 的,不会影响运行时性能,且不能用 || 来代替,且a\b\c 都是编译时确定值的变量,而非类型。

5、std::enable_if_t< some_value > 其实是 std::enable_if_t< some_value , void > 的简称,且 std::enable_if_t 的定义是:

复制代码
template <bool _Test, class _Ty = void>
using enable_if_t = typename enable_if<_Test, _Ty>::type;

enable_if<the_value, the_type> ::type 的 意思是

如果the_value是true,则定义为形式2,如果the_value是false,则定义为形式1 。

复制代码
template <bool _Test, class _Ty = void>
struct enable_if {}; // no member "type" when !_Test   //形式1

template <class _Ty>
struct enable_if<true, _Ty> { // type is _Ty for _Test  //形式2
    using type = _Ty;
};

此时根据 C++17模板元编程的原则 "SFINAE(Substitution Failure Is Not An Error,替换失败并非错误)" ,如果找不到 ::type ,那么就忽略掉模板函数,而非报错。

同时,enable_if_t 是 typename enable_if<_Test, _Ty>::type; 的昵称。因此 std::enable_if_t< some_value > 的意思是,如果有 some_value 的值,那么此处就以 void 来定义,如果没有,那么就当该函数 (函数1)不存在。

6、template <typename... BufferTypes, some_type * = nullptr>

由于5的论述,我们知道,some_type 只可能是 void ,否则把该模板函数(函数1)当做不存在。那么上述表达等价于:

template <typename... BufferTypes, void * = nullptr>

不妨补充其匿名的名称为 secretboy ,如下

template <typename... BufferTypes, ( void * ) secretboy = nullptr>

也就是说,逗号的后面,是一个值(而不是类型),且该值没有名字,它不会被函数体用到,那么它无关紧要。既然无关紧要,为啥还需要它呢?因为要让它的计算起到 SFINAE 的作用。所以,如果5合法,那么 它等价于

template <typename... BufferTypes >

如果5不合法,那么忽略函数1的存在。

7、 if constexpr (sizeof...(Buffers) > 0)

表示如果 Buffers 有真实的数量,那么 就需要编译下面的代码,否则不用编译。

8、(GetBufferCount(Forward<BufferTypes>(Buffers)) + ...)

我们把它简化为这个形式: (func(BufferTypes) + ... )

其中的 + ,在语法上还可以是 - (减号) || (或者),甚至是 , (逗号) 。

也就是说,函数调用后的 参数包 ,可以展开,等价于:

func(BT1) + func(BT2) + ... (式子8)

并且请注意, args + ... 的展开等价于 a + ( b + ( c + d ) ) ,所以严格来说,式子8不够严谨,应该等价于:

func(BT1) + ( func(BT2) + ( ... ) ) (式子9)

相关推荐
炸膛坦客9 小时前
单片机/C/C++八股:(二十六)IIC 专题(I²C)---- 上集
c语言·c++·单片机
旖-旎9 小时前
LeetCode 518:零钱兑换||(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题
Henry Zhu1239 小时前
C++中的特殊成员函数与智能指针
c++
_wyt00112 小时前
多重背包问题详解
c++·背包dp
Ljwuhe13 小时前
C++——多态
开发语言·c++
延凡科技13 小时前
多场景落地复盘:端边云架构无人机智能巡检系统设计与实践
大数据·数据结构·人工智能·科技·架构·无人机·能源
越甲八千13 小时前
STL stack为何没有迭代器
c++
从零开始的代码生活_15 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
云小逸15 小时前
【C++ 第七阶段:模板、泛型编程与工程综合详解】
开发语言·c++
GIS阵地15 小时前
QgsSingleBandPseudoColorRenderer 完整详解(QGIS 3.40.13 C++)
开发语言·前端·c++·qt·qgis