需要帮助破译模板元编程宏
In need of assistance deciphering template-metaprogramming macro
我非常想自己尝试这种技术,但是我在GCC和Clang上遇到了编译错误:
Undefined symbols for architecture x86_64:
"foo::_theInfo", referenced from:
foo::info() in reflect-eLsDqz.o
ld: symbol(s) not found for architecture x86_64
这是代码(直接来自博客上托管的zip),我希望Watte先生不介意。
/* A simple C++ reflection mark-up library. The strength
of this library is that it is entirely static (all the
reflection information is built without using dynamic
memory allocation), and it is exremely compact, allowing
you to declare the members of a structure right where
that struct is declared, leading to minimal risk of
version mismatch.
This code is placed in the public domain by Jon Watte.
http://www.enchantedage.com/cpp-reflection
Version 2009-04-20
*/
#include <typeinfo>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct TypeBase
{
virtual void Marshal(void *, void const *) const = 0;
virtual void Demarshal(void const *, void *) const = 0;
virtual char const *name() const = 0;
virtual size_t size() const = 0;
};
template<typename T> struct Type : TypeBase
{
static Type<T> instance;
// custom marshaling is handled by template specialization
void Marshal(void *dst, void const *src) const { memcpy(dst, src, sizeof(T)); }
void Demarshal(void const *src, void *dst) const { memcpy(dst, src, sizeof(T)); }
char const *name() const { return typeid(T).name(); }
size_t size() const { return sizeof(T); }
};
template <typename T> T& instance() {
static T t;
return t;
}
template<typename T, typename Q>
TypeBase *get_type(Q T::*mem) {
return &instance<Type<Q> >();
}
struct member_t
{
char const *name;
TypeBase *type;
size_t offset;
};
struct ReflectionBase
{
void ReflectionConstruct();
virtual size_t size() const = 0;
virtual char const *name() const = 0;
virtual size_t memberCount() const = 0;
virtual member_t const *members() const = 0;
};
void ReflectionBase::ReflectionConstruct()
{
// todo: register myself in some table, etc
members();
memberCount();
name();
size();
}
#define MEMBER(x)
{ #x, get_type(&T::x), (size_t)&((T*)0)->x },
#define RTTI(_type, _mems)
template<typename T> struct _info : ReflectionBase {
/* overrides used by ReflectionBase */
inline size_t size() const { return sizeof(T); }
inline char const *name() const { return #_type; }
inline size_t memberCount() const { size_t cnt; get_members(cnt); return cnt; }
inline member_t const *members() const { size_t cnt; return get_members(cnt); }
_info() { ReflectionConstruct(); }
static inline member_t const *get_members(size_t &cnt) {
static member_t members[] = { _mems };
cnt = sizeof(members) / sizeof(members[0]);
return members;
}
static inline _info<T> &info() {
return instance<_info<T> >();
}
};
inline static member_t const * members() { return _info<_type>::info().members(); }
static _info<_type> _theInfo;
inline static _info<_type> &info() { return _theInfo; }
struct foo {
int x, y, z;
RTTI(foo, MEMBER(x) MEMBER(y) MEMBER(z))
};
int main() {
printf("type: %sn", foo::info().name());
printf("size: %ldn", foo::info().size());
for (size_t i = 0; i != foo::info().memberCount(); ++i) {
printf(" %s: offset %ld size %ld type %sn",
foo::info().members()[i].name, foo::info().members()[i].offset,
foo::info().members()[i].type->size(), foo::info().members()[i].type->name());
}
return 0;
}
因此,static _info<_type> _theInfo
在上面的行中声明,但某些内容不起作用。我想我有点接近理解这段代码是如何工作的(MEMBER
是一个整洁的小宏,可以获取成员的偏移量,这很有趣),但我从来没有需要做一些像将模板化类定义放在宏中这样可怕的事情以前,所以我实际上不知道从哪里开始戳这个。
我找到了一个可以编译的不同版本。
Diffchecker是一个有用的工具。
inline static _info<_type> &info()
{
static _info<_type> _theInfo;
return _theInfo;
}
相关文章:
- 需要帮助设置在C++中使用的Potrace
- 在指针的帮助下,文本文件中单词的频率
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- 模板元编程:如何将参数包组合成新的参数包
- C 编程语言帮助我
- TIC TAC TOE编程帮助C
- 需要帮助破译模板元编程宏
- 我需要帮助来理解编程挑战
- C++游戏编程帮助.超级简单
- SDL C++游戏编程帮助(简单)
- 真的需要一些帮助,第一个编程项目
- 网络编程基础知识,需要帮助了解addrinfo/sockaddr_instructs的用法等
- 编程新手,类的问题,需要帮助来显示我哪里出了问题
- 帮助解决编程挑战
- 请帮助:使用 ADODB 在 Visual C++ 中进行遗留数据库编程
- C++编程语法和函数帮助