Search code examples
c++namespacesbackwards-compatibility

How do I put several functions into multiple namespaces without duplicating this code?


This is a followup to:

How do I put some code into multiple namespaces without duplicating this code?

I need to change the name of a namespace but want to keep backwards compatibility. The above solution suggests that I would have to do what is done there for each and every function:

namespace NewNamespaceName
{
        void print()
        {
                //do work...
        }
        // 50 other functions
}

namespace OldNameSpaceName
{
        using NewNamespaceName::print;
        // 50 other using declarations
}

My question: Is there a simpler way to do this?


Solution

  • You can simply do

    namespace NewNamespaceName
    {
            void print()
            {
                    //do work...
            }
            // 50 other functions
    }
    
    namespace OldNameSpaceName
    {
            using namespace NewNamespaceName;
    }
    

    If NewNamespaceName has other things you would want to include, but not want them to be in OldNamespaceName, then just make another private namespace and import that into the old namespace

    namespace NewNamespaceName
    {
            namespace Private {
                    void print() { ... }
                    // 50 other functions
            }
    }
    
    namespace OldNameSpaceName
    {
            using namespace NewNamespaceName::Private;
    }
    

    Live example here https://wandbox.org/permlink/taRPm1hAd2FHbAYo