用于查找参数是否为类的不同模板语法
template<typename> struct void_ { typedef void type; };
template<typename T, typename = void> // Line 1
struct is_class { static bool const value = false; };
template<typename T>
struct is_class<T, typename void_<int T::*>::type> { // Line 2
static bool const value = true;
};
此构造查找给定类型是否是类。让我困惑的是编写这个小元程序的新语法。谁能详细解释一下:
- 为什么我们需要1号线?
- 语法
作为 Line 中的template
参数 2 ?
While reading this question , I came across @Johannes's answer.
template<typename> struct void_ { typedef void type; };
template<typename T, typename = void> // Line 1
struct is_class { static bool const value = false; };
template<typename T>
struct is_class<T, typename void_<int T::*>::type> { // Line 2
static bool const value = true;
};
This construct finds if the given type is a class or not. What puzzles me is the new kind of syntax for writing this small meta program. Can anyone explain in detail:
- Why we need Line 1 ?
- What is the meaning of syntax
<int
as
T::*>template
parameter in Line
2 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第 1 行:如果测试成功,则选择下面的部分特化。
第 2 行:
int T::*
仅当T
是类类型时才有效,因为它表示成员指针。因此,如果它有效,则
void_::type
产生void
,并选择此部分特化来使用value
进行实例化true
。如果
T
不是类类型,那么由于 SFINAE,这种部分特化就无法实现,并且它默认返回到value
为false 的通用模板
。每次您看到
T::SOMETHING
时,如果SOMETHING
不存在,无论是类型、数据成员还是简单的指针定义,您都会启动 SFINAE。Line 1: Choosing the partial specialization below if the test succeeds.
Line 2:
int T::*
is only valid ifT
is a class type, as it denotes a member pointer.As such, if it is valid,
void_<T>::type
yieldsvoid
, having this partial specialization chosen for the instantiation with avalue
oftrue
.If
T
is not of class type, then this partial specialization is out of the picture thanks to SFINAE and it defaults back to the general template with avalue
offalse
.Everytime you see a
T::SOMETHING
, ifSOMETHING
isn't present, be it a type, a data member or a simple pointer definition, you got SFINAE going.1. 第 1 行用于非类的内容,例如 int、long 等...
例如:
因为存在部分特化,所以第 1 行是您必须拥有的,否则您将收到错误如果传递的类型不是类(例如 char *、long、int ...)
2:int T::* 的键是“::*”,它是 C++ 中的标准运算符,
表示指针指向类的成员,可以是两者函数或数据字段,
在这种情况下,它意味着任何拥有成员或可以使用成员指针的人,这仅适用于 C++ 中的类、结构或联合,因此结果是,您将知道参数是或不是一个类。
顺便说一句,谷歌一些关键字,例如:c++ template、部分专业化和类型特征或提升类型特征
希望这对你有用:)
1. line 1 is used for something which is not a class, like int, long and so on ...
for example:
because of there is a partial specialization so line 1 is what you have to have, otherwise you will get an error if you pass a type which is not a class (such as char *, long, int ...)
2: the key of int T::* is "::*", it is a standard operator in c++
means a pointer points to a member of a class, can be both a function or a data field,
and in this case it means anyone who has a member or can work with a member pointer, this is only works for classes, structs, or unions in c++, so the result of that, you will know the parameter is or not a class.
btw, google some keywords like: c++ template, partial specialization, and type traits, or boost type traits
hope this is useful for you :)