模板中不完整的类使用 [英] Incomplete class usage in template

查看:174
本文介绍了模板中不完整的类使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am very surprised that on various sampled versions of g++, the following compiles without error or warning:

// 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() {}

$ c> X 缺少或在不同的翻译单位,我会得到错误。

If the definition of X is missing or in a different translation unit, I do get errors.

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

But in the program as above, isn't the definition of f the single instantiation point of my template? And isn't the incompleteness of X at that instantiation point a semantic error?

03和/或C ++ 11草案)标准称这个程序形式良好,形态不良,形态不良,但诊断不需要,或未定义的行为?

Does the (C++03 and/or C++11 Draft) Standard call this program well-formed, ill-formed, ill-formed but diagnostic not required, or undefined behavior?

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

@David Rodriguez - dribeas reports that clang++, comeau, and Visual Studio 2010 also accept similar code.

推荐答案

到Alf Steinbach发布答案,但由于他没有这样做,我将在Lounge C ++聊天中发布他提到的引用):

(I was waiting to Alf Steinbach to post an answer, but since he is not doing it, I will post the reference that he mentioned in the Lounge C++ chat):

标准表示在翻译单元已经被翻译之后执行模板实例化,使得在所有非模板元素已经被处理之后,模板实例化在之后发生。这在2.2节翻译的描述:

The standard indicates that template instantiations are performed after the translation unit has already been translated, so that in time, template instantiations happen after all the non templated elements have already been processed. This is described in section 2.2 Phases of translation:

段落1-6定义了预处理器工作和基本的 操作(字符集的转换,字符串的连接...)

Paragraphs 1-6 define the preprocessor work and basic textual operations (conversions of the character set, concatenation of literals...)


7 /分隔令牌的空白字符不再重要。每个预处理令牌都将转换为令牌。 (2.7)。生成的令牌在句法和语义上被分析并翻译为翻译单元。

7/ White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. (2.7). The resulting tokens are syntactically and semantically analyzed and translated as a translation unit.

8 /翻译的翻译单元和实例化单元组合如下:产生所需实例化的列表。所需模板的定义位于。它是由实现定义的,包含这些定义的翻译单元的源是否需要可用。执行所有所需的实例化以产生实例化单元。 [注意:这些类似于翻译的翻译单位,但不包含对无意义模板的引用,也不包含模板定义。 - end note]如果任何实例化失败,该程序是错误的。

8/ Translated translation units and instantiation units are combined as follows: Each translated translation unit is examined to produce a list of required instantiations. The definitions of the required templates are located. It is implementation-defined whether the source of the translation units containing these definitions is required to be available. All the required instantiations are performed to produce instantiation units. [ Note: These are similar to translated translation units, but contain no references to uninstantiated templates and no template definitions. — end note ] The program is ill-formed if any instantiation fails.

为了简洁,我删除了一些注释。现在重要的一点似乎是代码被翻译而不需要在一个步骤中触发模板实例化,然后在稍后的步骤中实例化模板。这反过来意味着如果类型是完成在翻译单元中的任何地方,它将在编译器到实例化之前处理。

I have removed some of the notes for brevity. Now the important bit seems to be that the code is translated without triggering template instantiations in one step, and then in a later step the templates are instantiated. This in turn means that if the type is complete anywhere in the translation unit, it will have been processed by the time the compiler gets to the instantiation.

免责声明:这似乎是所有的编译器,我已经尝试显示完全相同的行为(gcc,clang,comeau,VS 2010)一个很好的理由,但这只是状态执行实例化时,它不会在模板实例化时显式地声明类型可能不完整。

Disclaimer: This seems like a good reason for all of the compilers that I have tried showing the exact same behavior (gcc, clang, comeau, VS 2010), but this only states when in time the instantiation is performed, it does not explicitly state that the type can be incomplete at the point of instantiation of the template.

这篇关于模板中不完整的类使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆