Frage

Is there any way to emulate an inline namespace with MSVC?

LLVM's libc++ uses this to create a hidden versioned namespace like so:

#define _LIBCPP_BEGIN_NAMESPACE_STD namespace std {inline namespace _LIBCPP_NAMESPACE {
#define _LIBCPP_END_NAMESPACE_STD  } }
#define _VSTD std::_LIBCPP_NAMESPACE
namespace std {
  inline namespace _LIBCPP_NAMESPACE {
  }
}

And emulates it on GCC like so:

#define _LIBCPP_BEGIN_NAMESPACE_STD namespace std { namespace _LIBCPP_NAMESPACE {
#define _LIBCPP_END_NAMESPACE_STD  } }
#define _VSTD std::_LIBCPP_NAMESPACE

namespace std {
namespace _LIBCPP_NAMESPACE {
}
using namespace _LIBCPP_NAMESPACE __attribute__((__strong__));
}

Now my question is, how do I achieve the same with MSVC? If it's not possible, I'll be happy with a solution that leaves out the versioning (for now), which I guess would be

#define _LIBCPP_BEGIN_NAMESPACE_STD namespace std {
#define _LIBCPP_END_NAMESPACE_STD }
#define _VSTD std

But kind of defeats the purpose...

War es hilfreich?

Lösung

I'm afraid there is no possibility of such emulation. Microsoft seems to be very uninterested in symbol versioning, even though they break ABI on their standard library on every single new revision of their compiler. The GCC emulation works because strong using was the basis for the inline namespace feature. Microsoft never had a similar thing, so you can't emulate inline namespaces. I'm afraid you're stuck with not versioning libc++ for now.

There is one feature in Microsoft's compiler that may help. This is #pragma detect_mismatch: http://msdn.microsoft.com/en-us/library/ee956429.aspx

Basically, you put

#pragma detect_mismatch("libcxx_version", "1.0")

into a central libc++ header file, and every module that includes that file will have a record placed in it that contains the key and value. The Microsoft linker checks, when linking modules, that all such records have the same value for any given name, and complains if there is a mismatch.

The end result is that you can't have multiple parallel versions of libc++ in a single program, but at least you won't get silent corruption from linking incompatible object files that cause nasty crashes at runtime.

Edit: forgot to mention: this feature is new in VS2010, but porting libc++ to a compiler without rvalue refs is probably rather hopeless anyway.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top