[英]'Creating pointer to member of non-class type 'T*'' compiler error
In the code below, I am trying to specialize FieldCompare template 在下面的代码中,我试图专门化FieldCompare模板
struct A
{
size_t key;
};
template <class T, class Field, Field T::*field_ptr>
class FieldCompare
{
};
template <class T, class Field, Field T::*field_ptr>
class FieldCompare<T *, Field, field_ptr>
{
};
int main()
{
FieldCompare<A *, size_t, &A::key> comp;
return 0;
}
but getting 'creating pointer to member of non-class type 'T*'' compiler error. 但得到'创建指向非类型成员的指针'T *''编译器错误。
what is wrong in this code? 这段代码有什么问题?
EDIT1: EDIT1:
See the life example of what I am trying to do. 看看我想要做的生活例子 。 It is a specialization of a comparer for using with std::set and std::set, for example 例如,它是与std :: set和std :: set一起使用的比较器的特化
The issue here is that you cannot identify a member of T
through T::*ptr
when T
is a pointer. 这里的问题是,当T
是指针时,你无法识别T
到T::*ptr
的成员。 With the <type_traits>
header, however, you can get around this by having the base template this way: 但是,使用<type_traits>
标头,您可以通过以下方式使用基本模板来解决此问题:
#include <type_traits>
template <class T, class Field, Field std::remove_pointer_t<T>::*field_ptr>
class FieldCompare
{ /* ... */ };
and the specialized version 和专业版
template <class T, class Field, Field std::remove_pointer_t<T>::*field_ptr>
class FieldCompare<T*, Field, field_ptr>
{ /* ... */ };
Only having the std::remove_pointer_t
in the partial specialization doesn't work, as this wouldn't be a specialization. 仅在部分std::remove_pointer_t
中使用std::remove_pointer_t
不起作用,因为这不是专门化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.