为什么不能在模板参数中将指针downcast到成员? [英] Why can't I downcast pointer to members in template arguments?

查看:181
本文介绍了为什么不能在模板参数中将指针downcast到成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用指针到基本成员,我可以将其转换为指针到派生成员通常,但不是当使用模板,如Buzz下面,其中第一个模板参数影响第二个。我是打架编译器错误还是标准确实要求这不工作?

If I make a pointer-to-base-member, I can convert it to a pointer-to-derived-member usually, but not when used within a template like Buzz below, where the first template argument influences the second one. Am I fighting compiler bugs or does the standard really mandate this not work?

struct Foo
{
    int x;
};

struct Bar : public Foo
{
};

template<class T, int T::* z>
struct Buzz
{
};

static int Bar::* const workaround = &Foo::x;

int main()
{
    // This works. Downcasting of pointer to members in general is fine.
    int Bar::* y = &Foo::x;

    // But this doesn't, at least in G++ 4.2 or Sun C++ 5.9. Why not?
    // Error: could not convert template argument '&Foo::x' to 'int Bar::*'
    Buzz<Bar, &Foo::x> test;

    // Sun C++ 5.9 accepts this but G++ doesn't because '&' can't appear in
    // a constant expression
    Buzz<Bar, static_cast<int Bar::*>(&Foo::x)> test;

    // Sun C++ 5.9 accepts this as well, but G++ complains "workaround cannot
    // appear in a constant expression"
    Buzz<Bar, workaround> test;

    return 0;
}


推荐答案

。根据§14.3.2/ 5:

It simply isn't allowed. According to §14.3.2/5:


对用作非类型模板参数的每个表达式执行以下转换。如果非类型模板参数不能转换为相应的模板参数的类型,那么程序是不成形的。

- 对于整型或枚举类型的非类型模板参数,整数提升(4.5)和积分转换(4.7)。

- 对于指向对象的非类型模板参数类型指针,限定转换(4.4)和数组到指针转换4.2)。
- 对于对象的引用类型的非类型模板参数,不适用转换。引用所引用的类型可能比模板参数的(否则相同的)类型更cv-qualified。模板参数直接绑定到模板参数,它必须是一个左值。

- 对于指向函数的类型指针的非类型模板参数,只有函数到指针的转换4.3)。如果template-argument表示一组重载函数(或指向这样的指针),则从集合(13.4)中选择匹配函数。

- 对于类型为reference的非类型模板参数功能,不适用转换。如果template-argument表示一组重载函数,则从集合(13.4)中选择匹配函数。

- 对于成员函数指针类型的非类型模板参数,不应用转换。如果template-argument表示一组重载的成员函数,则从集合(13.4)中选择匹配成员函数。

- 对于类型为指向数据的类型的模板参数

我强调了有关指向数据成员的指针的转换。请注意,您的转换(§4.11/ 2)未列出。在C ++ 0x中,在这方面保持不变。

I've emphasized the conversion regarding pointer to data members. Note that your conversion (§4.11/2) is not listed. In C++0x, it remains the same in this regard.

这篇关于为什么不能在模板参数中将指针downcast到成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆