Skip to content

Commit

Permalink
Add KAITAI_STREAM_H_CPP11_SUPPORT macro, fix C++98 compatibility
Browse files Browse the repository at this point in the history
See #72 (comment)

The standard library header `<type_traits>` is only available since
C++11 (see https://en.cppreference.com/w/cpp/header/type_traits), so we
must not try to include it in C++98 mode.
  • Loading branch information
generalmimon committed Apr 30, 2024
1 parent 7795aef commit 69cb210
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion kaitai/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// achieve that: C++98 compilers prefer `throw()`, C++11 and later
// use `noexcept`. We define KS_NOEXCEPT macro for that.

#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
#ifdef KAITAI_STREAM_H_CPP11_SUPPORT
#define KS_NOEXCEPT noexcept
#else
#define KS_NOEXCEPT throw()
Expand Down
11 changes: 9 additions & 2 deletions kaitai/kaitaistream.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
// Kaitai Struct runtime API version: x.y.z = 'xxxyyyzzz' decimal
#define KAITAI_STRUCT_VERSION 11000L

// check for C++11 support - https://stackoverflow.com/a/40512515
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
#define KAITAI_STREAM_H_CPP11_SUPPORT
#endif

#include <stdint.h> // int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t

#include <ios> // std::streamsize
#include <istream> // std::istream
#include <limits> // std::numeric_limits
#include <sstream> // std::istringstream
#include <string> // std::string

#ifdef KAITAI_STREAM_H_CPP11_SUPPORT
#include <type_traits> // std::enable_if, std::is_integral
#endif

namespace kaitai {

Expand Down Expand Up @@ -229,8 +237,7 @@ class kstream {
* since C++11) in older C++ implementations.
*/
template<typename I>
// check for C++11 support - https://stackoverflow.com/a/40512515
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
#ifdef KAITAI_STREAM_H_CPP11_SUPPORT
// https://stackoverflow.com/a/27913885
typename std::enable_if<
std::is_integral<I>::value &&
Expand Down

0 comments on commit 69cb210

Please sign in to comment.