理解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)

相关推荐
肆忆_20 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星1 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
琢磨先生David5 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马5 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
qq_454245035 天前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝5 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode