Question

When writing non-member, free functions, can they be placed in the global namespace so long as the signature specifies a namespace-scoped object? For example, in the code below, is "Example 2" acceptable design? Which is better, Example 1 or Example 2? Does your answer change if the function is an operator overload?

Research: I don't think this question was addressed when I took a C++ programming class (or it may have been addressed before I was ready to understand it). I tried searching for the answer with a few different permutations of the keywords but did not get any quality hits.

#include <iostream>
#include <string>
#include <sstream>

/* Example 1 */
namespace myapp
{
    namespace xyz
    {
        class Thing
        {
        public:
            Thing( int value ) : myValue( value ) {}
            void setValue( int value ) { myValue = value; }
            int getValue() const { return myValue; }
        private:
            int myValue;
        };
        std::string toString( const Thing& thing )
        {
            std::stringstream ss;
            ss << thing.getValue();
            return ss.str();
        }
    }
}

/* Example 2 */
namespace myapp
{
    namespace xyz
    {
        class AnotherThing
        {
        public:
            AnotherThing( int value ) : myValue( value ) {}
            void setValue( int value ) { myValue = value; }
            int getValue() const { return myValue; }
        private:
            int myValue;
        };
    }
}
std::string toString( const myapp::xyz::AnotherThing& thing )
{
    std::stringstream ss;
    ss << thing.getValue();
    return ss.str();
}

int main(int argc, const char * argv[])
{
    /* Example 1 */
    myapp::xyz::Thing t( 1 );
    std::cout << myapp::xyz::toString( t ) << std::endl;

    /* Example 2 */
    myapp::xyz::AnotherThing a( 2 );
    std::cout << toString( a ) << std::endl;

    return 0;
}
Was it helpful?

Solution

boost uses a lot of free functions (free functions are a good thing). The free functions are maintained close to the namespace that involves the objects or other related classes associated with the free function. Then the free function definitions are hoisted to upper namespaces as required.

This technique gives control over the scope of free functions. “Utility” free functions can be defined in a namespace then not hoisted treating them as “private-like” functions.

In this case, Example 1 is more appropriate choice but adding hoisting declarations in the outer namespaces will allow the functions to be more easily referenced.

OTHER TIPS

Put the function in the same namespace as the classes it operates on.

This way, it does not clutter the global namespace and is properly grouped for easy understanding, but will easily be found by ADL (argument-dependent-lookup).

As an aside, that's the way swap should be implemented and used:
The standard provides the generic std::swap and various specialized variants in the namespace of the objects it operates on, and depends on the overload-set consisting of the generic version, and whatever ones are found by ADL giving the right one.

Licensed under: CC-BY-SA with attribution
scroll top