【发布时间】:2016-12-29 10:27:57
【问题描述】:
我正在尝试实现一个“属性”系统来将 C++ 实例转换为 JSON,反之亦然。我从 Guillaume Racicot 在这个问题 (C++ JSON Serialization) 的回答中提取了部分代码并对其进行了简化。
这是我的操作方式。我有一个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;
};
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 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
是否有解决方法使其在 Visual Studio 2015 Update 3 中工作?
【问题讨论】:
-
Property构造函数接受一两个参数吗? -
@pts 是的,我把代码放在这里:coliru.stacked-crooked.com/a/276ac099068579fd
-
完全相同的代码在VS2015(更新3)中无法编译
-
我认为 MSVC 在完全定义类之前不能看到它的内部(
User::age在解析};之前是未知的)。 -
至于MSVC或GCC是否正确,阅读2个答案here
标签: c++ c++11 visual-c++ visual-studio-2015 c++14