是否有可能在C++ 11中写入(S,f)或hasfield(S,f)的宏存在?

Quu*_*one 7 c++ macros template-meta-programming c++11

我对标准元编程解决方案(例如C++ 11查找类型是否具有成员函数或支持运算符的方法)非常熟悉,这些解决方案不涉及宏魔术.但是,我有一个涉及以下便利宏的用例(当然,对于StackOverflow大大简化了,但想象这是用于序列化的东西)......

#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();
Run Code Online (Sandbox Code Playgroud)

但现在让我们说有人出现并在我们的架构中添加了一种新的数据成员:字段对(x, xLength).我们想要像这样改变我们的便利宏......

#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();
Run Code Online (Sandbox Code Playgroud)

就我自己而言,我设法提出了以下hasfield对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);                                        \
    }()
Run Code Online (Sandbox Code Playgroud)

...但不幸的是,这似乎是由于Clang的一个错误 ; 根据C++ 11标准,X不允许本地类具有模板成员.实际上,这段代码无法与GCC一起编译.

所以我很难过:在C++ 11中是否有可能定义OUTPUT宏以便它能做我想做的事情?

绝对约束:不改变结构的定义Baz.fieldname提前没有硬编码.

Nice-to-haves:一个hasfield(c,f)可以在其他上下文中使用的宏(而不是将代码直接缠绕到OUTPUT宏中).不,假设offsetof(c,fLength)==offsetof(c,f)+sizeof(std::declval<c>().f).

小智 3

current通过继承和依赖影子,可以使其在某些限制下工作,这些限制对您来说可能很重要,也可能不重要:声明一个局部fieldname变量,创建一个从您正在检查的类型派生的局部类,并在成员函数内部,检查是否fieldname仍然引用局部变量。如果是,则不fieldname存在任何成员。

#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();
}
Run Code Online (Sandbox Code Playgroud)

已编辑以在 GCC 4.6.3 上工作。它仍然被 GCC 4.8.1 和 clang 3.3 接受,并且也应该与 GCC 4.7.3 兼容(但不能与 4.7.2 兼容)。