tags:

views:

92

answers:

1

While reading Karlsson's Beyond the C++ Standard the author defined the friend function intrusive_ptr_add_ref in the body of class reference_counted (see pg 36). That function is called automatically using Argument Dependent Lookup at the proper time.

Never having seen friend functions defined in the body of a class, I played around and discovered that gcc 4.4.3 requires a forward declaration if not using ADL lookup. In fact, there seems to be no way to reference adl_no without that forward declaration. Is this part of the C++ standard or is it an artifact of gcc? (I don't have Windows box so cannot try VC).

#include <cstdlib>
#include <iostream>


namespace {
    void adl_no();        // Remove this and it won't compile with gcc

    struct Q {
        friend void adl_yes(const Q&) {
            std::cout << "adl_yes" << std::endl;
        }

        friend void adl_no() {
            std::cout << "adl_NO" << std::endl;
        }
    };
}


int main(int argc, char** argv)
{
    adl_yes(Q());
    adl_no();

    return EXIT_SUCCESS;
}
+5  A: 

Yes, this behaviour is standard. The relevant part of the standard is 7.3.1.2 [namespace.memdef] para 3:

If a friend declaration in a non-local class first declares a class or function the friend class or function is a member of the innermost enclosing namespace. The name of the friend is not found by simple name lookup until a matching declaration is provided in that namespace scope [...]. If a friend function is called, its name may be found by the name lookup that considers function from namespaces and classes associated with the types of the function arguments (3.4.2) [i.e. ADL].

Charles Bailey
Cute. Moving the declaration of adl_no() from before to after the definition of struct Q works. Truly a maze of twisty little passages all alike.
themis
@themis: Yes they can tight and twisty when you use the obscure parts of the language (like defining a friend function inside the class).
Martin York