Skip to content

Commit

Permalink
Added shared_ptr handling for C++03 mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
madmaxoft committed Apr 25, 2014
1 parent 4f49c97 commit f3dfc03
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,15 @@ template class SizeChecker<UInt16, 2>;
// Allow both Older versions of MSVC and newer versions of everything use a shared_ptr:
// Note that we cannot typedef, because C++ doesn't allow (partial) templates to be typedeffed.
#if (defined(_MSC_VER) && (_MSC_VER < 1600))
// MSVC before 2010 doesn't have std::shared_ptr, but has std::tr1::shared_ptr
// MSVC before 2010 doesn't have std::shared_ptr, but has std::tr1::shared_ptr, defined in <memory> included earlier
#define SharedPtr std::tr1::shared_ptr
#else
// All others have std::shared ptr
#elif (__cplusplus > 201103L)
// C++11 has std::shared_ptr in <memory>, included earlier
#define SharedPtr std::shared_ptr
#else
// C++03 has std::tr1::shared_ptr in <tr1/memory>
#include <tr1/memory>
#define SharedPtr std::tr1::shared_ptr
#endif


Expand Down

12 comments on commit f3dfc03

@madmaxoft
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have done the trick. Now I'm only worried about MacOS compilation, I remember having some <tr1/memory> issues there. Can anyone with a mac try this out?

@archshift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Building with the Makefile fails:

Scanning dependencies of target iniFile
[  0%] Building CXX object lib/inifile/CMakeFiles/iniFile.dir/iniFile.cpp.o
In file included from xxx/xxx/MCServer/lib/inifile/iniFile.cpp:18:
xxx/xxx/MCServer/lib/inifile/../../src/Globals.h:280:11: fatal error:
      'tr1/memory' file not found
        #include <tr1/memory>
                 ^
1 error generated.
make[2]: *** [lib/inifile/CMakeFiles/iniFile.dir/iniFile.cpp.o] Error 1
make[1]: *** [lib/inifile/CMakeFiles/iniFile.dir/all] Error 2
make: *** [all] Error 2

@archshift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Building with XCode fails:

OSSupport Group
xxx/xxx/MCServer/src/Globals.h
xxx/xxx/MCServer/src/Globals.h:280:11: 'tr1/memory' file not found
HTTPServer Group
xxx/xxx/MCServer/src/Globals.h
xxx/xxx/MCServer/src/Globals.h:280:11: 'tr1/memory' file not found

@archshift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind OSX uses Clang, not GCC.

@madmaxoft
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If OSX uses clang, then it should be using the c++11 flag, so it should take the C++11 path in the middle. Weird. But I expected some problems with this, we've hit this before. Any recommendations on how to #ifdef this?

@archshift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My compiler's __cplusplus is at 199711 without std=c++11. With, it's at 201103, but it'll still end up at #else.

#elif (__cplusplus > 201103L) should be #elif (__cplusplus >= 201103L), and cmake needs to set the c++11 flag when it can for XCode.

@madmaxoft
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to use the C++11 flag as little as we can because we still support C++03 compilers (VC2008). I guess I've borked the #elif, will try >=

@archshift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the issue. You need to add add_flags_cxx("-stdlib=libc++") after line 191 of SetFlags.cmake, as well as the >= to the elif.

@madmaxoft
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too comfortable forcing flags on the compiler that I don't know what they exactly do. If it works with only the comparison change, then I'd prefer to leave everything else as-is.

@archshift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the comparison change, the Makefile generator will compile correctly, but not XCode. That flag in SetFlags.cmake is only applied for Clang, and switches the standard library to use C++11. As far as I can tell, that switch is required in order for the __cplusplus macro to be at least 201103L.

@archshift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have to test building with Clang on Linux as well, probably in a VM.

@archshift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.