Josh
Josh

Reputation: 6272

Visual C++ Standard Library keywords

I wanted to write a unicode version of std::exception and std::runtime_error.
So I thought what better way then to just take implementations from the C++ Standard Library and alter them to support unicode.

So I pulled up the exception and stdexcept headers in Visual C++, copied the code, made my changes.

The thing is I couldn't get it to link unless I removed the _CRTIMP_PURE. I also removed the _EXCEPTION_INLINE __CLR_OR_THIS_CALL prefix from all the member functions.

It's working but I'm very curious what all those things did.
_EXCEPTION_INLINE it literally defined right above it as #define _EXCEPTION_INLINE, and my googling skills can't find any documentation on what they do.

So, does anyone know what these are meant to do? And why it wound't link until I removed the _CRTIMP_PURE prefix from the class?

Upvotes: 1

Views: 355

Answers (2)

Michael Burr
Michael Burr

Reputation: 340198

These aren't really anything mysterious (but it might be a bit pf a pain to track down where they're defined - but only a little). They're defined in headers that are part of the library, and they take on different definitions depending on how the compiler is configured for the current run. In particular, these macros seem to be concerned mostly with whether or not the current run is configured for /clr:pure.

_CRTIMP_PURE is defined to __declspec(dllimport) if you're linking against the DLL version of the C runtime (and not building with /clr:pure), and defined to nothing otherwise.

If your library isn't a DLL (or if it won't necessarily be a DLL whenever the DLL runtime is configured), then you shouldn't use it. You probably shouldn't use it anyway, because you'd need to define it differently when building your library than when your library is being used (that's what Microsoft does when they build the C runtime libraries).

__CLR_OR_THIS_CALL is used by Microsoft's libraries to declare a function with __clrcall if you're building with /clr:pure (indicating these functions will only be called by managed code - the compiler can perform certain optimizations in that case it seems).

Finally, _EXCEPTION_INLINE is used to make the member functions of class exception inline if building with /clr:pure.

So the bottom line is, don't use __CLR_OR_THIS_CALL or _EXCEPTION_INLINE unless you plan to support /clr for your library, and you probably shouldn't use _CRTIMP_PURE in your implementation, but probably should use something similar of your own making and under your own control.

Upvotes: 2

Emile Cormier
Emile Cormier

Reputation: 29209

Standard library implementation code often uses compiler-specific extensions for performance/documentation/reliability reasons. You should not use these extensions in your own code, because they can break in subsequent versions of the compiler.

There's no problem in duplicating the interface of std::exception and friends, and you can look at Visual Studio's implementation for inspiration. But your implementation should only use publically documented language/library features.

Upvotes: 1

Related Questions