【发布时间】:2012-01-16 22:54:51
【问题描述】:
我想实现像 sizeof(complete_type) 将返回实际 sizeof 和 sizeof(incomplete_type) - 将只是 0 的行为
我需要它来为 IPC(进程间)通信提供扩展的运行时类型信息,每个类型都有描述结构:
struct my_type_info
{
bool is_pointer;
size_t size; //for double* will be 4 on i386. that is sizeof(double*)
size_t base_size; //for double* will be 8. that is sizeof(double)
};
当进入我的系统时出现问题,例如 class MyOnlyDeclaredClass;我得到了编译错误,显然是因为我无法确定它的大小。
boost type_traits http://www.boost.org/doc/libs/1_48_0/libs/type_traits/doc/html/index.html 建议许多编译时类,但没有'is_incomplete'
有趣的编译器是 VS2008、VS2010、clang 3、gcc-4.6、gcc-4.7
【问题讨论】:
-
如果检测到类型不完整怎么办?抛出 static_assert 错误?
-
不,如果指针位于共享内存中,我可以将它传递给另一个进程而不知道它是什么类型。指针可以通过从其值中减去共享内存基数来轻松传递。
-
我好像还是没看懂。为什么不以这种方式传递所有指针(包括完整类型和不完整类型)?
-
因为在我的系统中提供了另一种指针——比如应该以另一种方式传递给另一个进程的接口。此外,它将允许在运行时检查指针来自哪种内存。我承认有能力以另一种不太优雅的方式解决任务。
-
请告诉我们更多您真正需要做的事情。
标签: c++ templates incomplete-type template-argument-deduction