在命名空间中的Friend函数声明/定义 [英] Friend function declaration/definition inside a namespace

查看:467
本文介绍了在命名空间中的Friend函数声明/定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个命名空间中的类。类的定义声明一个朋友函数。

Consider a class inside a namespace. The definition of the class declares a friend function.

namespace Foo
{
    class Bar
    {
        friend void baz();
    };
}

这应该基于我所知道的,声明 baz()作为最内层命名空间的成员,即 Foo

This should, based on what I know, declare baz() as a member of the innermost enclosing namespace, i.e. Foo.

因此,我希望 baz()的以下定义是正确的:

Therefore, I expected the following definition for baz() to be correct:

void Foo::baz() { }

error: ‘void Foo::baz()’ should have been declared inside ‘Foo’

几种解决方案似乎有效:

Several solutions seem to work:


  • 在类外部声明 baz()

namespace Foo
{
    void baz();

    class Bar
    {
        friend void baz();
    };
}


  • 定义 baz c $ c>命名空间中。

  • Define baz() inside the namespace.

    namespace Foo
    {
        class Bar
        {
            friend void baz();
        };
    }
    ...
    namespace Foo
    {
        void baz() { }
    }
    


  • 使用 -ffriend-injection 标志进行编译, >


  • Compile with the -ffriend-injection flag, which eliminates the error.

    这些解决方案似乎与C ++中声明/定义的一般规则不一致。

    These solutions seem to be inconsistent with the general rules of declaration/definition in C++ I know.

    为什么我必须声明 baz()两次?

    为什么定义在命名空间中是合法的,

    Why do I have to declare baz() twice?
    Why is the definition otherwise only legal inside a namespace, and illegal with the scope resolution operator?
    Why does the flag eliminate the error?

    推荐答案


    为什么我必须声明baz()两次?

    Why do I have to declare baz() twice?

    因为朋友声明不提供可用的声明的命名空间中的函数。它声明,如果该函数在该命名空间中声明,它将是一个朋友;如果您要在类中定义友元函数,那么它将通过参数相关查找(但不是其他方式),就像在命名空间中声明的一样。

    Because the friend declaration doesn't provide a usable declaration of the function in the namespace. It declares that, if that function is declared in that namespace, it will be a friend; and if you were to define a friend function inside a class, then it would be available via argument-dependent lookup (but not otherwise) as if it were declared in the namespace.


    为什么这个定义在命名空间中是合法的,并且是范围解析操作符的非法?

    Why is the definition otherwise only legal inside a namespace, and illegal with the scope resolution operator?

    因为它没有(正确)在命名空间中声明,并且只有在已声明的情况下,才能在其命名空间之外定义一个函数(具有范围解析)。

    Because it hasn't been (properly) declared in the namespace, and a function can only be defined outside its namespace (with scope resolution) if it has been declared.


    为什么标志消除了错误?

    Why does the flag eliminate the error?

    朋友声明作为命名空间中的声明。这是为了与古代方言的C ++(和显然,一些现代编译器)的兼容性,其中这是标准的行为。

    Because the flag causes the friend declaration to act as a declaration in the namespace. This is for compatibility with ancient dialects of C++ (and, apparently, some modern compilers) in which this was the standard behaviour.

    这篇关于在命名空间中的Friend函数声明/定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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