如果没有编译失败,是否有可能推断出类型是否完整?

dev*_*ull 5 c++ templates incomplete-type template-argument-deduction

我想实现像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)
};
Run Code Online (Sandbox Code Playgroud)

进入我的系统时出现类似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

cur*_*guy 11

不要试图这样做.

它根本不健全.模板按类型进行参数化,而不是实例化点.类类型本身不完整或不完整,它在翻译期间的某个时刻完成.

在某些类型上实例化的模板必须在每个实例中具有完全相同的语义.

如果不是,则不定义行为.

  • +1.我也正要那样尖叫.从根本上打破了这个想法. (5认同)

ken*_*ytm 4

像往常一样使用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 };
};
Run Code Online (Sandbox Code Playgroud)

例子:

#include <cstdio>

struct F;
struct G {};

int main()
{
    printf("%d %d\n", is_complete<F>::value, is_complete<G>::value);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

(注意:适用于gcc 4.5(不,这不是因为 C++0x)和 clang 2.9,但不适用于 gcc 4.3