2

According to "Which <type_traits> cannot be implemented without compiler hooks?", it's not possible to implement is_pod in C++03 without compiler intrinsics. I thought of a possible way, though: C++03 does not allow unions that have members with a nontrivial constructor. Is it possible to use SFINAE to detect this without throwing a compiler error?

I tried the following and many variants, and couldn't get it to work. It always either throws an error or returns invalid answers.

#include <cstdio>

template <typename T>
class is_pod
{
  template <typename U, void (U::*)()> struct Check;
  template <typename U> union Union { U u; void member(); };
  template <typename U> static char func(Check<Union<U>, &Union<U>::member> *);
  template <typename U> static char (&func(...))[2];
public:
  enum { value = sizeof(func<T>(0)) == sizeof(char) };
};

struct Meow { };

struct Purr { Purr() { } };

int main()
{
  std::printf("%d\n", is_pod<Meow>::value);
  std::printf("%d\n", is_pod<Purr>::value);
  return 0;
}
Luis Machuca
  • 1,047
  • 9
  • 16
Myria
  • 3,372
  • 1
  • 24
  • 42

0 Answers0