Search code examples
c++linkerfriend

How to make a friend function have internal linkage


I need to ensure that a C++ function, declared as a friend of certain C++ class, has internal linkage.

The reason why I need the function to be a friend is because it needs access to a private member of that class, a private member which caches the function's result.

The reason why the function needs to be non-member (or at least, not an instance member) of that same class is because other code needs to be able to take a non-member function pointer to it. Refactoring this would be too expensive.

The reason why I need it to have internal linkage is because there will be lots of these functions and, on AIX, having too many of them causes the TOC overflow error when linking. This can be overcome using the -bbigtoc linker switch, but I'm trying to avoid this for the moment.

Also, I would really like to be able to keep the class declaration in the header file but put the function itself in the .cxx file that contains the implementation.

To sum it up, right now I have something like this:

class Foo
{
    private:
        mutable Qux cachedBarResult;
    // ... more code here ...
    public:
        friend const Qux & Bar(const Foo &);
};

const Qux & Bar(const Foo & foo)
{
    if (foo.cachedBarResult.isEmpty())
    {
        foo.cachedBarResult = _worker_Bar(foo);
    }
    return foo.cachedBarResult;
}

static Qux _worker_Bar(const Foo & foo)
{
    // real Bar work
}

I want to make Bar have internal linkage. Can that be done?


Solution

  • Yep, you just have to declare the static function's prototype before you say it's a friend.

    class Foo;
    static const Qux & Bar(const Foo & foo);
    
    class Foo
    {
        private:
            mutable Qux cachedBarResult;
        // ... more code here ...
        public:
            friend const Qux & Bar(const Foo &);
    };
    
    
    static const Qux & Bar(const Foo & foo)
    {
        if (foo.cachedBarResult.isEmpty())
        {
            foo.cachedBarResult = _worker_Bar(foo);
        }
        return foo.cachedBarResult;
    }