4

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;
}

2 Answers 2

2

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.

2
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.