了解C++ 98中的Enable_if实现
我已经将其视为enable_if
for c ++ 98
的自称实现:
template<bool b, typename T = void>
struct enable_if {
typedef T type;
};
template<typename T>
struct enable_if<false, T> {};
但是,我个人不明白。我看不到布尔人在哪里开始玩游戏。如果有人会为我解开它,真的很感激。
I have seen this given as a self-explanatory implementation of enable_if
for C++98
:
template<bool b, typename T = void>
struct enable_if {
typedef T type;
};
template<typename T>
struct enable_if<false, T> {};
But alas I personally don't understand it. I don't see where the boolean kicks into play. Would really appreciate if someone would unwrap it for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先考虑这一点:
它是主要模板和专业化。在一般情况下,
foo&lt; b&gt; :: b
只是b
。在特殊情况下,当b == false
专业启动时,foo&lt; :: b
isfalse
。您的
std :: enable_if
的示例有两个不同的原因:a)它正在使用部分专业化。专业化是针对任何类型的t
和b == false;
。 b)在专业中,没有类型
成员别名。这就是std :: enable_if
的全部目的。当条件为false时,std :: enable_if&lt;条件,t&gt; ::类型
是一个替代故障,因为专业化没有type
。当条件
是true
然后std :: enable_if; condition t&gt; :: type
只是t
。First consider this:
Its a primary template and a specialization. In the general case
foo<b>::B
is justb
. In the special case whenb == false
the specialization kicks in andfoo<false>::B
isfalse
.Your example of
std::enable_if
is different for two reasons: A) It is using partial specialization. The specialization is for any typeT
andb == false;
. B) in the specialization there is notype
member alias. And thats the whole purpose ofstd::enable_if
. When the condition is false thenstd::enable_if< condition, T>::type
is a substitution failure, because the specialization has notype
. When thecondition
istrue
thenstd::enable_if<condition,T>::type
is justT
.