C++模板元编程中,如何通过std::enable_if实现长尾函数式编程的模板条件判断机制?
- 内容介绍
- 文章标签
- 相关推荐
本文共计731个文字,预计阅读时间需要3分钟。
C++11 引入了 std::enable_if 函数,用于在模板函数中实现条件编译。以下是一个简化版的示例:
cpptemplatestruct enable_if {};
templatestruct enable_if { typedef T type;};
C++11中引入了std::enable_if函数,函数原型如下:
template< bool B, class T = void > struct enable_if;
可能的函数实现:
template<bool B, class T = void> struct enable_if {}; template<class T> struct enable_if<true, T> { typedef T type; };
由上可知,只有当第一个模板参数为true时,enable_if会包含一个type=T的公有成员,否则没有该公有成员。
本文共计731个文字,预计阅读时间需要3分钟。
C++11 引入了 std::enable_if 函数,用于在模板函数中实现条件编译。以下是一个简化版的示例:
cpptemplatestruct enable_if {};
templatestruct enable_if { typedef T type;};
C++11中引入了std::enable_if函数,函数原型如下:
template< bool B, class T = void > struct enable_if;
可能的函数实现:
template<bool B, class T = void> struct enable_if {}; template<class T> struct enable_if<true, T> { typedef T type; };
由上可知,只有当第一个模板参数为true时,enable_if会包含一个type=T的公有成员,否则没有该公有成员。

