指向成员的指针:在GCC中工作,但不在VS2015中 [英] Pointer to member: works in GCC but not in VS2015

查看:189
本文介绍了指向成员的指针:在GCC中工作,但不在VS2015中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个属性系统来将C ++实例转换为JSON,反之亦然。我参考了Guillaume Racicot在这个问题中的回答( C ++ JSON序列化)的一部分代码,简化了它。



这是我如何进行。我有一个属性类:

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

T类:: * m_member;
const char * m_name;
};

m_member指向 Class



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

  class User 
{
public:
int age;

constexpr static auto properties = std :: make_tuple(
Property< User,int>(& User :: age,age)
);这个代码在GCC(http://coliru.stacked-crooked.com/a/276ac099068579fd ),但不是在Visual Studio 2015更新3.我得到这些错误: / p>

  main.cpp(19):错误C2327:'User :: age':不是类型名称,静态或枚举
main.cpp(19):error C2065:'age':undeclared identifier
main.cpp(20):error C2672:'std :: make_tuple':找不到匹配的重载函数
main.cpp(20):error C2119:'properties':'auto'的类型不能从空的初始化器中推导


b $ b

有没有解决方法,使其工作在Visual Studio 2015更新3?

解决方案

只是用属性成员函数替换属性成员数据:

  class User 
{
public:
int age;

constexpr static auto properties(){return std :: make_tuple(
Property< User,int>(& User :: age,age)
); }
};

这是因为在成员函数的定义中,类被认为是完全定义的。它还具有期望的属性,如果使用odr,属性不需要单独定义。


I'm trying to implement a "property" system to convert C++ instances into JSON and vice versa. I took a part of the code from Guillaume Racicot's answer in this question (C++ JSON Serialization) and simplified it.

Here is how I proceed. I have a Property class:

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;
};

m_member points to a specific member of Class

Let's say I want to define properties for a User class, I would like to be able to proceed like this, to be able to assign members a property name:

class User
{
public:
    int age;

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

This code compiles and works correctly in GCC(http://coliru.stacked-crooked.com/a/276ac099068579fd) but not in Visual Studio 2015 Update 3. I get those errors:

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

Would there be a workaround to make it work in Visual Studio 2015 Update 3?

解决方案

My preferred workaround is just to replace the properties member data with a properties member function:

class User
{
public:
    int age;

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

This works because in the definition of a member function, the class is considered to be completely defined. It also has the desirable attribute that properties doesn't need to be separately defined if odr-used.

这篇关于指向成员的指针:在GCC中工作,但不在VS2015中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆