简体   繁体   English

'创建指向非类型成员'T *''编译器错误的成员

[英]'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是指针时,你无法识别TT::*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.

相关问题 非类类型错误和指针问题 - Non-class type error, and Pointer issues 为什么在尝试 class 成员指针时 SFINAE 技巧不适用于非类类型? - Why SFINAE trick doesn't work for non-class type when tried for class member pointer? 避免非类类型的指向成员函数 - avoid pointer-to-member-function for non-class type 获取编译器错误:在 pq 中请求成员推送,它是非类类型 std::priority_queue - Getting compiler error: Request for member push in pq, which is of non-class type std::priority_queue 调用成员类时发生非类类型错误 - Non-class type error on invocation of a member class 类成员函数导致错误“非类类型” - class member function resulting in error “non-class type” 非类类型&#39;char&#39;的成员打印错误请求 - Error request for member print which is of non-class type 'char' 错误:请求的成员属于非类类型&#39;&#39;的成员&#39;&#39; - Error: Request for member ' ' in ' ' , which is of non-class type ' ' 对非类类型&#39;char&#39;的成员getName的错误请求 - Error request for member getName which is of non-class type 'char' 错误:请求“graph”中的成员“clear”,这是非类类型 - error: request for member 'clear' in 'graph', which is of non-class type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM