作为好友的模板参数

发布于 2024-11-17 08:20:52 字数 169 浏览 5 评论 0原文

在 C++03 中,以下内容是非法的,尽管某些编译器支持它。

template <class T>
class X
{
    friend T;
};

这在 C++11 中合法化了吗? (抱歉,我自己没有时间阅读草稿,只是希望有人知道这一点)

In C++03 the following is illegal, although some compilers support it.

template <class T>
class X
{
    friend T;
};

Has this been legalized in C++11? (Sorry, didn't have time to read the draft myself, just hoping someone knows this)

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

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

发布评论

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

评论(3

遥远的她 2024-11-24 08:20:52

来自 N3291 中的第 §11.3, 3 节:

template <typename T> class R {
  friend T;
};

R<C> rc; // class C is a friend of R<C>
R<int> Ri; // OK: "friend int;" is ignored

因此它在 C++11 中是合法的。

From section §11.3, 3 in N3291:

template <typename T> class R {
  friend T;
};

R<C> rc; // class C is a friend of R<C>
R<int> Ri; // OK: "friend int;" is ignored

So it is legal in C++11.

离旧人 2024-11-24 08:20:52

是的,c++0x 允许模板参数成为朋友。

好吧,我碰巧记得之前在草稿中读过它,但找不到参考文献..无论如何@Praetorian 的答案钉住了它。

Yes c++0x allows template parameter to be friends.

Well, I happened to remember read it in the draft before but could not find the reference..anyways @Praetorian's answer nailed it.

审判长 2024-11-24 08:20:52

在纯 C++ 中这是非法的,但有一个简单的解决方法

template <class T>
class X
{
    private:
        class Wrapper
        {
            public:
                typedef T Type;
        };
        friend class Wrapper::Type;
};

It is illegal in plain C++, but there is a simple workaround

template <class T>
class X
{
    private:
        class Wrapper
        {
            public:
                typedef T Type;
        };
        friend class Wrapper::Type;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文