mercredi 22 février 2017

How do I reliably detect support for nullptr?

I need to backport nullptr to a cross platform library we have, but I'm having trouble getting a reliable check of nullptr support.

Initially I had this:

#if __cplusplus >= 201103L || (__cplusplus < 200000 && __cplusplus >= 199711L)
    // nullptr should be ok
#else
    // Too old
#endif

But then I discovered that compiling a program that just printed out the value of __cplusplus produced unexpected results.

199711L was a value that MS compilers use to indicate partial support for C++11. But I notice g++ 5.4 produces that value by default. Until you expressly tell it to use compile with -std=c++11. But then if you tell it the standard is c++98 the value 199711 is still shown. That doesn't sound right to me.

Then I saw someone doing this

#if !defined(nullptr)
#endif

But I'm not sure you can do that. So I tested it like this:

#if defined(nullptr)
#error "null ptr defined"
#endif

Guess what? that doesn't print out the error when nullptr is actually available. So that doesn't seem to work at all.

How do I detect nullptr or compiler version under linux/windows and OSX (clang)/ android.

Aucun commentaire:

Enregistrer un commentaire