[Bug c++/32204] friend from global namespace in template class ignored

scottbaldwin at gmail dot com gcc-bugzilla@gcc.gnu.org
Sat Oct 27 08:12:00 GMT 2012


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32204

etherice <scottbaldwin at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |scottbaldwin at gmail dot
                   |                            |com

--- Comment #7 from etherice <scottbaldwin at gmail dot com> 2012-10-27 08:11:45 UTC ---
As Jonathan explains in comment #5, gcc is right to reject this code and MSVC
is wrong to accept it. You will have to add a forward declaration of the
class/function in order to declare it as a friend in the namespaced class. For
example:

// on gcc 4.7.0 (linux), these forward declarations are required for the friend
declarations in ns::NamespacedClass
class GlobalClass;
void globalFunction();

namespace ns {
    struct NamespacedClass {
        friend ::GlobalClass; // same result whether '::' or 'class' is part of
declaration
        friend void ::globalFunction();
    private:
        NamespacedClass() {}
    };
}

struct GlobalClass {
    GlobalClass() { ns::NamespacedClass foo; }
};

void globalFunction() {
    ns::NamespacedClass foo;
}



More information about the Gcc-bugs mailing list