指向成员的指针:在GCC中工作但在VS2015中不起作用

Mic*_*ael 14 c++ visual-c++ c++11 c++14 visual-studio-2015

我正在尝试实现一个"属性"系统来将C++实例转换为JSON,反之亦然.我在这个问题(C++ JSON序列化)中从Guillaume Racicot的答案中获取了部分代码并对其进行了简化.

以下是我的进展方式.我有一Property节课:

template <typename Class, typename T>
struct Property {
    constexpr Property(T Class::* member, const char* name) : m_member(member), m_name(name) {}

    T Class::* m_member;
    const char* m_name;
};
Run Code Online (Sandbox Code Playgroud)

m_member指向特定成员 Class

假设我想为User类定义属性,我希望能够像这样继续,以便能够为成员分配属性名称:

class User
{
public:
    int age;

    constexpr static auto properties = std::make_tuple(
        Property<User, int>(&User::age, "age")
    );
}
Run Code Online (Sandbox Code Playgroud)

此代码在GCC(http://coliru.stacked-crooked.com/a/276ac099068579fd)中编译并正常工作,但在Visual Studio 2015 Update 3中没有.我收到了以下错误:

main.cpp(19) : error C2327 : 'User::age' : is not a type name, static, or enumerator
main.cpp(19) : error C2065 : 'age' : undeclared identifier
main.cpp(20) : error C2672 : 'std::make_tuple' : no matching overloaded function found
main.cpp(20) : error C2119 : 'properties' : the type for 'auto' cannot be deduced from an empty initializer
Run Code Online (Sandbox Code Playgroud)

是否有解决方法使其在Visual Studio 2015 Update 3中运行?

Okt*_*ist 6

我首选的解决方法是properties使用properties成员函数替换成员数据:

class User
{
public:
    int age;

    constexpr static auto properties() { return std::make_tuple(
        Property<User, int>(&User::age, "age")
    ); }
};
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为在成员函数的定义中,该类被认为是完全定义的.它还具有理想的属性,properties如果使用odr则不需要单独定义.