我无法访问指向成员的指针。为什么?

发布于 2024-08-12 10:21:48 字数 609 浏览 1 评论 0原文

考虑以下代码:

template<class T, class F>           struct X {};
template<class T, class F, T F::* m> struct Y {};

struct Foo {
    int member;
    typedef X<int, Foo>               x_type; // works well
    typedef Y<int, Foo, &Foo::member> y_type; // ERROR
};

typedef Y<int, Foo, &Foo::member> y_type2; // OK

为什么编译器会生成错误? (VS2008)


我已将此错误发布到 connect.microsoft.com

Consider the following code:

template<class T, class F>           struct X {};
template<class T, class F, T F::* m> struct Y {};

struct Foo {
    int member;
    typedef X<int, Foo>               x_type; // works well
    typedef Y<int, Foo, &Foo::member> y_type; // ERROR
};

typedef Y<int, Foo, &Foo::member> y_type2; // OK

Why does compiler generate error? (VS2008)


New

I have posted this bug to connect.microsoft.com.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转瞬即逝 2024-08-19 10:21:48

我认为这与 Visual C++ 不知道此时指向成员的指针的大小有关。例如,检查缺陷报告 (这里是指向成员变量的指针的另一个问题)。我认为您又发现了一个 Visual C++ 错误,应该将其报告给 connect.microsoft.com。

I think that it is related somehow with that Visual C++ don't know the size of pointer to member at that point. Check this defect report for instance (here is another problem with pointer to member variable). I think that you found one more Visual C++ bug and it should be reported to connect.microsoft.com.

柏林苍穹下 2024-08-19 10:21:48

这是一个错误

This is a bug

逆夏时光 2024-08-19 10:21:48

我偶然发现了同样的问题。 VC++ 中对指向成员模板参数的支持仍然有限(请参阅错误报告) 。

就我而言,我可以通过使用模板函数 iso 模板类来解决它:

template< typename Class > struct CMemberDumper {
    Class& object;
    template< typename M > void visit_member( M C::*pm ) {
       std::cout << object.*pm;
    }
};

I stumbled upon the same problem. The support for pointer-to-member template arguments is still limited in VC++ (see bug report).

In my case I could work around it by using a template function i.s.o. a template class:

template< typename Class > struct CMemberDumper {
    Class& object;
    template< typename M > void visit_member( M C::*pm ) {
       std::cout << object.*pm;
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文