[英]std::enable_if with sizeof compile hack
不确定以下代码中的sizeof(Input)
是如何编译的。 我认为那里也有一些逗号运算符的魔力,但不确定。 我知道SFINAE
需要在直接上下文中发生,这就是我对typename F = T
所做的,但不确定为什么(A)
有效而(B)
失败。
#include <iostream>
#include <type_traits>
using namespace std;
template <typename T>
struct Test {
template <typename Input,typename F = T, enable_if_t<std::is_same_v<F,int>, bool>* = nullptr>
// template <typename Input, enable_if_t<(sizeof(Input),std::is_same_v<T,int>), bool>* = nullptr> // Works, but how? (A)
// template <typename Input, enable_if_t<std::is_same_v<T,int>, bool>* = nullptr> (B)
void func(Input a) {
cout << "INT" << endl;
}
template <typename Input,typename F = T, enable_if_t<!std::is_same_v<F,int>, bool>* = nullptr>
// template <typename Input, enable_if_t<(sizeof(Input),!std::is_same_v<T,int>), bool>* = nullptr> // Works, but how? (A)
// template <typename Input, enable_if_t<!std::is_same_v<T,int>, bool>* = nullptr> // Doesnt work (B)
void func(Input a) {
cout << "NON INT" << endl;
}
};
int main(){
Test<int> t;
t.func(5);
}
事实上,(A) 也是格式不正确的 NDR,但编译器无法成功诊断。
一旦T
在 class 中固定,就没有使模板有效的Input
。
它似乎有效。 随着表达式变得依赖于模板,第一阶段的检查更加复杂,只有第二阶段(在替代期间)失败但不是错误(适用 SFINAE)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.