How can I put multiple functions in multiple namespaces without duplicating this code?

This is the following:

How do I put some code in multiple namespaces without duplicating that code?

I need to change the namespace name but want to keep backward compatibility. The above solution assumes that I would have to do what there is for each function:

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

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

      

My question is, is there an easier way to do this?

+3


source to share


2 answers


You can just do

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

namespace OldNameSpaceName
{
        using namespace NewNamespaceName;
}

      

If NewNamespaceName

there are other things that you would like to include but do not want them in OldNamespaceName

, just create another private namespace and import it into the old namespace



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

namespace OldNameSpaceName
{
        using namespace NewNamespaceName::Private;
}

      

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

+1


source


Just use a namespace alias:



namespace OldNameSpaceName = NewNameSpaceName;

      

+6


source







All Articles