lundi 23 octobre 2023

Why would non-inline namespaces not work for library versioning?

In this answer to this question What are inline namespaces for?, inline namespaces are referred to as a mechanism of library versioning.

I understand the overall approach mentioned in the answers using inline namespace, but one thing that is not clear is why could we not use the following approach for library versioning with non-inline namespaces?

In the first version, have a definition like the following.

#include <iostream>

namespace foo {
    template <typename T>
    void bar(T t) {
        std::cout << "bar v1 - " << t;
    }
}

And in the next version, place the older version in a nested namespace v1 and the newer version in its place.

#include <iostream>

namespace foo {
    namespace v1 {
        template <typename T>
        void bar(T t) {
            std::cout << "bar v1 - " << t;
        }
    }

    template <typename T>
    void bar(T t) {
        std::cout << "bar v2 - " << t;
    }

}

All correct usages of foo::bar() will automatically get upgraded to v2, and would work correctly against v2. For any users of foo::bar(), if they still want to link to v1 then they can change the code to foo::v1::bar(), just as is suggested in the linked answer to the question, where they have used inline namespaces for the solution.

What is wrong or missing or would not work, with the above approach that doesn't use inline-namespaces?

Aucun commentaire:

Enregistrer un commentaire