如何称呼表达式"T (&some(...))[2]' 其中 T=字符

What to call the expression `T (&some(...)) [2]` where T=char

本文关键字:字符 其中 some 表达式      更新时间:2023-10-16

我在库实现中看到过这个表达式,我基本上理解它被用来培养 SFINAE,甚至拉动static_assert触发器。

它基本上采用以下形式:

template <typename>
char (&checkValid(...))[2];
template <typename T>
char checkValid(e); where e is an expression(using type T) results in type X 

如果e格式正确,那么结果将是(假设使用sizeof(1 其他 2,并且可以应用于:

static_assert(sizeof(checkValid<T>(0))==1,"") ;

前几天我一直在以不同的方式做类似的事情:

using namespace std;
template<typename...T>
using isValid = void;
template<typename>
false_type checkValid(...);
template<typename T>
true_type checkValid(isValid<typename T::type>*);
struct some{
using type = int;
};
int main(){
constexpr bool result = decltype(checkValid<some>(0))::value;
}

不管我做了什么,看到了什么,我更想知道:

这个表达式叫什么?

template <typename>
char (&checkValid(...))[2];

"变量模板"?"函数模板?"还是"引用...的数组"?(对不起,如果我的猜测很糟糕(

它是一个函数模板,返回对char[2]的引用。

checkValid           // `checkValid` is
checkValid(...)      // a function with (...) parameter list, returning
&checkValid(...)      // a reference to
(&checkValid(...))     // (discard parentheses)
(&checkValid(...))[2]  // an array of 2
char (&checkValid(...))[2]  // characters.
template <typename> char (&checkValid(...))[2]; // And it's a template.
相关文章: