3

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...

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
rubenvb
  • 74,642
  • 33
  • 187
  • 332

1 Answers1

8

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.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • You're in the lead for now ;). It is a different method to accomplish essentially the same result. – rubenvb Oct 17 '11 at 16:09
  • I see two possible issues/shortcomings with this: 1) how does this pragma affect client code? How would the use of this pragma in client code for their versioning purposes be limited? 2) The inline namespace approach was introduced (I think) to provide a means of future ABI compatibility, which would ideally keep the old symbols around (I may be mistaken though, it seems logical to do so). I fear this won't be sufficient for those purposes and defeat the purpose of versioning at all. – rubenvb Oct 19 '11 at 10:54
  • 1) Not sure what you mean. You can use the pragma as often as you want, as long as the key is different. The linker requires that all key-value pairs match. – Sebastian Redl Oct 23 '11 at 10:46
  • 2) Correct, inline namespaces allow side-by-side versions, pragma detect_mismatch does not. It only offers protection against linking together mismatched versions. Given that I have been bitten by that in the past, though, I'm grateful that we now have at least that from MS. – Sebastian Redl Oct 23 '11 at 10:48