Q:

Is it possible to write a macro existenceof(S,f) or hasfield(S,f) in C++11?

I'm reasonably familiar with the standard metaprogramming solutions (e.g. C++11 ways of finding if a type has member function or supports operator?) which don't involve macro magic. However, I have a use-case involving the following convenience macros (drastically simplified for StackOverflow, of course, but imagine that this is for serialization or something)...
#define START(type) do { typedef type current; const char typeName[] = #type
#define OUTPUT(fieldname) \
    printf("type of %s.%s is %s\n", #type, #fieldname, \
        std::is_same<decltype(std::declval<current>().fieldname),int> ? "int" : "string")
#define END() } while (0)

struct Foo { int i; char *j; char *k; };
struct Bar { char *x; int y; };

START(Foo);
  OUTPUT(i);  // type of Foo.i is int
  OUTPUT(j);  // type of Foo.j is string
  OUTPUT(k);  // type of Foo.k is string
END();
START(Bar);
  OUTPUT(x);  // type of Bar.x is string
  OUTPUT(y);  // type of Bar.y is int
END();

But now let's say someone comes along and adds a new kind of data member to our schema: pairs of fields (x, xLength). We want to change our convenience macros like this...
#define START(obj) do { const auto& current = (obj)
#define OUTPUT(fieldname) \
    printf("type of %s.%s is %s\n", #type, #fieldname, \
        std::is_same<decltype(std::declval<current>().fieldname),int> ? "int" :
        hasfield(current, fieldname##Length) ? "Pascal string" : "C string")
#define END() } while (0)

struct Baz { char *x, *y, *z; int xLength, zLength; };

START(Baz);
  OUTPUT(x);  // type of Baz.x is Pascal string
  OUTPUT(y);  // type of Baz.y is C string
  OUTPUT(z);  // type of Baz.z is Pascal string
END();

On my own I managed to come up with the following implementation of hasfield that works on Clang...
#define hasfield(classtype, fieldname)                                        \
    []() {                                                                    \
        struct X {                                                            \
            template<class T, int=sizeof(&T::fieldname)> static constexpr bool f(T*){ return true; } \
            static constexpr bool f(...) { return false; }                    \
        }; return X::f((classtype*)0);                                        \
    }()

...but unfortunately this appears to be due to a bug in Clang; according to the C++11 standard, local class X isn't allowed to have template members. Indeed, this code fails to compile with GCC.
So I'm stumped: Is it possible in C++11 to define the OUTPUT macro such that it'll do what I want?
Absolute constraints: No changing the struct definition of Baz. No hard-coding the fieldname ahead of time.
Nice-to-haves: A hasfield(c,f) macro that can be used in other contexts as well (as opposed to entwining the code directly into the OUTPUT macro). No assuming that offsetof(c,fLength)==offsetof(c,f)+sizeof(std::declval<c>().f).

A:

It's possible to get this to work with some restrictions that may or may not matter to you by inheriting from current and relying on shadowing: declare a local fieldname variable, create a local class that derives from the type you're checking, and inside a member function, check whether fieldname still refers to the local variable. If it does, no member fieldname exists.
#include <utility>
#include <stdio.h>

#define START(type) do { typedef type current; const char typeName[] = #type
#define HASMEMBER(fieldname) \
    []() -> bool { \
        struct HASMEMBER1 { } fieldname; \
        struct HASMEMBER2 : current { \
             static char TEST1(HASMEMBER1&); \
             static char (&TEST1(...))[2]; \
             auto TEST2() -> decltype(TEST1(fieldname)); \
        }; \
        return sizeof(std::declval<HASMEMBER2>().TEST2()) == 2; \
    }()
#define OUTPUT(fieldname) \
    printf("type of %s.%s is %s\n", typeName, #fieldname, \
        std::is_same<decltype(current::fieldname),int>::value ? "int" : \
        HASMEMBER(fieldname##Length) ? "Pascal string" : "C string")
#define END() } while (0)

struct Foo { int i; char *j; char *k; };
struct Bar { char *x; int y; };
struct Baz { char *x, *y, *z; int xLength, zLength; };

int main()
{
START(Foo);
  OUTPUT(i);  // type of Foo.i is int
  OUTPUT(j);  // type of Foo.j is C string
  OUTPUT(k);  // type of Foo.k is C string
END();
START(Bar);
  OUTPUT(x);  // type of Bar.x is C string
  OUTPUT(y);  // type of Bar.y is int
END();
START(Baz);
  OUTPUT(x);  // type of Baz.x is Pascal string
  OUTPUT(y);  // type of Baz.y is C string
  OUTPUT(z);  // type of Baz.z is Pascal string
END();
}

Edited to work on GCC 4.6.3. It is also still accepted by GCC 4.8.1 and clang 3.3, and should work with GCC 4.7.3 too (but not 4.7.2).

