Using TR1 libraries in GCC and MSVC - c++

I would like to use the TR1 libraries that ship with modern versions of GCC and MSVC, but there are subtle differences: in GCC, I have to say
#include <tr1/memory>
std::tr1::shared_ptr<int> X;
while in MSVC I have to say
#include <memory>
std::shared_ptr<int> X;
I have two questions: 1) Does MSVC automatically operate in C++0x-mode (equivalent to GCC's std=c++0x), or does it also work in C++98/03 mode by default? 2) How can I unify the includes and namespaces? I was thinking about a preprocessor macro of the sort "INCLUDE_TR1(memory)" or something like that.
To clarify, I want to use the traditional, standard C++98/03; not C++0x (otherwise there'd be no problem).
I'd be most grateful for any suggestions!

VC++ 2010 only operates in C++0x mode; previous versions had no C++0x support. That said, much of the standard library in VC++ 2010 is is still based on TR1 (e.g. std::result_of<> uses the TR1 result_of protocol instead of being decltype-based); in fact, much of the standard library in VC++ 2010 is not actually defined in namespace std, but rather in namespace std::tr1 and pulled into namespace std with a using directive.
Use Boost.TR1 -- it will #include the appropriate headers according to your platform, or if your platform doesn't have TR1 support, #include the corresponding Boost implementations and pull them into namespace std::tr1 with using declarations.

VC++ 2010 always operates in C++0x mode, but the classes exist in both the std and std::tr1 namespaces. You’ll have to detect the compiler with an #if _MSC_VER to choose which headers to include (see this answer).
The Boost.TR1 library can automatically include your compiler’s headers and fill in any missing functionality using Boost. It might help.

OK, after having several inconsistent and unsurmountable problems with Boost.TR1, especially when trying to use GCC's native TR1 libraries, I decided to ditch Boost entirely and use a small #define workaround. Here is my "tr1.h":
#ifndef _TR1_INCLUDE_H
#define _TR1_INCLUDE_H
/** Usage: #include TR1INCLUDE(unordered_map)
**
** Configuration: Define HAVE_TR1_SUBDIR if you need #include <tr1/unordered_map>; otherwise we take #include <unordered_map>.
**
**/
#define QUOTE(arg) <arg>
#ifdef HAVE_TR1_SUBDIR
# define TR1IFY(arg) tr1/arg
#else
# define TR1IFY(arg) arg
#endif
#define TR1INCLUDE(arg) QUOTE(TR1IFY(arg))
#endif
Now I can just write my programs like this:
#include "tr1.h"
#include TR1INCLUDE(unordered_map)

The different versions of MSVC have the features they have. There is no way of turning them on or off.
Some of them might also have both tr1 and std versions of some features. With slightly different semantics!

Related

Is it safe and legitimate to use std::experimental features in a library released to the public?

Suppose I'm writing a library which targets C++14-capable compilers.
In C++14, several standard library facilities were introduced as experimental, e.g. optional and filesystem.
Is it:
safe (for downstream developers)
legitimate
for me to utilize these features in my library even if only C++14 is supported? e.g. in the form
#if __cplusplus >= 201703L
#include <optional>
namespace mylib {
using std::optional;
using std::nullopt;
}
#else
static_assert(__cplusplus >= 201402L, "C++2014 is required to compile this program");
#include <experimental/optional>
namespace mylib {
using std::experimental::optional;
using std::experimental::nullopt;
}
#endif
Note: Of course, I mean in the case of downstream developers actually having to use these features themselves, not just for the case when I use them internally only with no outside exposure.

Does Visual Studio provide the C library’s isnormal()?

I’m building a C++ library with Visual Studio Express 2013. I’d like to call the isnormal function from cmath, but the VS library only seems to have _isnan and _finite (and they’re in cfloat, not cmath).
It also provides _fpclass, which I assume corresponds to fpclassify. Is the following macro a suitable replacement for isnormal?
#ifdef _WIN32
#define isnormal(x) (_fpclass(x) == _FPCLASS_NN || _fpclass(x) == _FPCLASS_PN)
#endif
According to this blog post isnormal is at least present in MSVC2013's <math.h>. Therefore, in <cmath> you should find the corresponding std::isnormal (You remembered to use namespace std, right?). If not, you'll have to fall back to the C header.
(Or maybe fix/update your MSVC installation - MGetz' comment shows that his VS2013 Express installation has std::isnormal)

How to #include hash with ext, tr1, or __gnu_cxx in XCode, C++

I'm trying to work with the google-sparsehash library and I'd like to include the hash library described in the link,
using ext::hash; // or __gnu_cxx::hash, or maybe tr1::hash, depending on your OS
and I've tried one of each:
#include <ext/hash>
#include <ext>
#include <__gnu_cxx>
#include <tr1>
which none worked with XCode. I've also "using", where I was told that __gnu_cxx does not contain "hash". How do I describe this library to XCode (3.2.6) on OS X (10.6.8)?
Or more generally, where is this hash function described in a Mac / XCode?
In C++11:
#include <functional>
using std::hash;
In C++03 with TR1:
#include <tr1/functional>
using std::tr1::hash;
So far as I can tell, it doesn't seem possible to get at the hash functors without also pulling in definitions for the various hash tables. At least not without fooling around with library internal headers.
Try:
#include <ext/hash_map>
using __gnu_cxx::hash;
or:
#include <tr1/unordered_map>
using std::tr1::hash;

Inline namespace emulation for MSVC (10.0/11.0)

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

Visual studio C++: How to make parts of code not be seen by the windows compiler?

So jenerally I have small C++ project based on OpenSource crossplatform libs. So it probably would compile under linux. So I hited the point when I need to implement some defenatly platform specific class functions.
I have a class header with all functions declarations and cpp file with realisations. So first: how to declare my platform specific functions in header so when I'll try to compile under linux it will not try to compile windows specific ones... and when on windows compiler will not try to compile linux functions include headers etc.
So for windows I need some how wrap such super specific functions
HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum)
void DisplayDeviceInformation(IEnumMoniker *pEnum)
And some headers
#include <windows.h>
#include <dshow.h>
#pragma comment(lib, "strmiids")
While for linux I have such headers
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev.h>
And I have function with name of void PrintCamerasList() which I wanna have one for bouth platfrms realisations for which I have seprate.
I hope you see what I need. So generally I need some example using my functions or once you can invent - let your imagination flow!)
So why do I need it all - I am creating some console app using OpenCV and I need to list user cameras names. OpenCV cannot do this on its own. so I asked how to do it for bouth platforms of my intrest - windows and linux
You want to look into platform specific macros and surround for example your MSVC specific code with some #ifdef _WIN32 / #endif pairs.
Take a look at http://predef.sourceforge.net/ for an extensive list of pre-defined macros various compilers provide to distinguish between operating systems, compilers, and processor architectures. They will allow you to distinguish between more than just Win32 and Linux if necessary.
Common practice is to use a compiler flag such as
#ifdef WIN_32
// Windows stuff...
#else
// Linux stuff
#endif
Check for the exact values of what windows flag is defined either in your compiler or in the headers you include