是否有可能在没有编译失败的情况下推断类型是否不完整
Is it possible to deduce whether type is incomplete without compilation failure?
我想实现这样的行为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)
};
问题出现时,进入我的系统像类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
不要这么做。
这根本是不合理的。模板是通过类型参数化的,而不是实例化点。类类型不完整或本身不完整,它在翻译过程中的某个时刻是完整的。
在某些类型上实例化的模板在每次实例化时必须具有完全相同的语义。
像往常一样使用SFINAE。这是一种可能的实现:
struct char256 { char x[256]; };
template <typename T>
char256 is_complete_helper(int(*)[sizeof(T)]);
template <typename>
char is_complete_helper(...);
template <typename T>
struct is_complete
{
enum { value = sizeof(is_complete_helper<T>(0)) != 1 };
};
的例子:
#include <cstdio>
struct F;
struct G {};
int main()
{
printf("%d %dn", is_complete<F>::value, is_complete<G>::value);
return 0;
}
(注意:适用于gcc 4.5(不,不是因为c++ 0x)和clang 2.9,但不适用于gcc 4.3)
相关文章:
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在这种情况下,java对象是否可以调用本机函数
- C++标准是否允许<double>在没有开销的情况下实现 std::可选
- C++默认情况下,指针类型数组的元素是否保证初始化为 nullptr?
- 是否可以在没有真实文件的情况下创建 ifstream
- 是否可以在不使用 decltype 的情况下推断先前定义的 extern 变量的类型
- 我是否在C++中投下新的结果?
- 是否可以在不填充自己的参数的情况下将模板函数作为参数传递?
- C++:带有大括号初始化列表的函数调用表达式 - 标准是否规定在单个元素列表的微不足道的情况下忽略大括号?
- 是否可以在不扣除的情况下将模板参数转发到 make_*?
- C++ 移动语义是否在任何情况下都能节省资源?
- 如何检查流中文件的下一个单词是否为 alpha?
- 在这种情况下,使用 string_view 是否会导致不必要的字符串复制?
- 我是否访问了已释放的内存,或者在这种情况下DrMemory报告不正确?
- G++ 编译器是否在未使用返回值的情况下将 constexpr 函数视为常规函数?
- 在没有任何锁的情况下加入另一个线程后是否需要内存围栏?
- 有没有办法检查发送到变量的值是否是正确的类型,而它已经在该变量下?
- 下推解析器是否扫描令牌或单个字符
- 我们可以在不使用构造函数的情况下推回包含矢量的结构
- 如何设计下推自动机