相关疑难解决方法(0)

模板中的类使用不完整

我很惊讶在g ++的各种采样版本中,以下编译没有错误或警告:

// Adapted from boost::checked_delete()
template <class T> inline void assert_complete()
{
  typedef char type_must_be_complete[ sizeof(T) ? 1 : -1 ];
  (void) sizeof(type_must_be_complete);
}

class X;

void f()
{
  assert_complete<X>();
}

class X {};

int main() {}
Run Code Online (Sandbox Code Playgroud)

如果X缺少定义或在不同的翻译单元中,我会收到错误.

但是在上面的程序中,是不是f我的模板的单个实例化点的定义?X那个实例化的不完整性是不是语义错误?

(C++ 03和/或C++ 11草案)标准是否将此程序称为格式良好,格式错误,格式错误但不需要诊断或未定义的行为?

编辑:@David Rodriguez - dribeas报告clang ++,comeau和Visual Studio 2010也接受类似的代码.

c++ templates incomplete-type

14
推荐指数
2
解决办法
1475
查看次数

标签 统计

c++ ×1

incomplete-type ×1

templates ×1