3

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?

Fabian
  • 4,001
  • 4
  • 28
  • 59
  • 2
    Put all in a unique namespace, and `using` the full namespace (probably `inline`). – Jarod42 Jul 06 '17 at 12:00
  • If you have symbols in `NewNamespaceName` that should *not* be in `OldNamespaceName` then no you have no other choice. Possibly you could automate it with a script. – Some programmer dude Jul 06 '17 at 12:03

2 Answers2

6

Just use a namespace alias:

namespace OldNameSpaceName = NewNameSpaceName;
Useless
  • 64,155
  • 6
  • 88
  • 132
1

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

Curious
  • 20,870
  • 8
  • 61
  • 146
  • `Private` should be an `inline` namespace, though, or there should at least be a `using namespace Private;` inside `NewNamespaceName`. – Angew is no longer proud of SO Jul 06 '17 at 12:07
  • I like that very much! Can I add `using namespace Private;` within NewNamespaceName? Then all shared functions would be accessible via `NewNamespaceName::print()` and `OldNameSpaceName::print()`, right? Or will `inline namespace Private` do the same thing? – Fabian Jul 07 '17 at 05:25
  • @Fabian yep! you can do that if you want. I updated my answer to include this link at the end https://wandbox.org/permlink/taRPm1hAd2FHbAYo, take a look! – Curious Jul 07 '17 at 06:57