You don't need anything like this for C++
You don't need anything like this for C++
Posted Jul 4, 2012 18:16 UTC (Wed) by jwakely (subscriber, #60262)In reply to: You don't need anything like this for C++ by cmccabe
Parent article: Calling for a new openSUSE development model
foo(0x123456789LL) will give you the version1 functionAre you sure? I don't understand your example, as there isn't actually an inline namespace there, but if
version2 is meant to be inline, then both calls to foo would get the float version.
i.e. adding your version2 would have change the API in incompatible ways, as the original foo would no longer be used by anyone. You could do that, but it's probably not what you want.If you use a using declaration instead of a using directive then both overloads are found and overload resolution picks the best one.
Using declarations work better with inline namespaces. Consider version 1 of the API:
namespace api {
inline namespace v1 {
void foo(int) {
// implementation
}
void bar() {
// implementation
}
}
}
Code that calls api::foo(1) links to api::v1::foo(int).
Later, version 2 is released:
namespace api {
namespace v1 { // not inline now
// as before
}
inline namespace v2 {
void foo(int i) {
// improved v2 implementation
}
void foo(float) {
// new impl for float
}
using v1::bar;
}
}
Code that was linked to api::v1:foo still finds that symbol, with the same implementation. Code that is compiled against the new version and calls api::foo(1) links to api::v2::foo(int). Both symbols can coexist in the library (just as with ELF symbol versioning.) Code that calls api::bar() gets api::v1::bar() in both cases, so there's no duplication (except for adding a using declaration to the v2 namespace.)
