简体   繁体   English

为什么这段代码不会产生编译错误?

[英]Why doesn't this code generate compilation errors?

template<class T>
void foo()
{
    M
}

Unless I instantiate it, Visual C++ does not tell me the above code contains error(s). 除非我实例化它,否则Visual C ++不会告诉我上面的代码包含错误。 Why is this? 为什么是这样?

Because Visual C++ is in error. 因为Visual C ++出错了。 It doesn't implement two-phase lookup . 它不实现两阶段查找 It's supposed to check the template for proper syntax even if you don't instantiate it, but it doesn't do that. 它应该检查模板的正确语法,即使你没有实例化它,但它不会这样做。

GCC doesn't accept it. 海湾合作委员会不接受。 Not saying that necessarily means it's not right, but there's an example for you anyway of what's supposed to happen. 不是说这必然意味着它是不对的,但无论如何,无论如何都会有一个例子。

The C++ Standard contains an informal description of this matter which describes what I think would be a good guideline and is taken by many people to be the normative requirement implied by the specification. C ++标准包含对此问题的非正式描述,描述了我认为是一个很好的指导方针,并被许多人视为规范所暗示的规范性要求。

However, implementations can point to normative parts of the Standard that allow them to go further than what the "obvious" rules seem to state. 但是,实现可以指向标准的规范部分,使它们比“明显的”规则似乎更加明确。 I will describe this below. 我将在下面描述。

Non-Normative description 非规范性描述

The Standard allows an implementation not to check template definitions until they are instantiated. 标准允许实现在实例化之前不检查模板定义。 It doesn't give a formal description as to when a "template definition" is actually supposed to be a template definition, but the usual implementation is to do forms of "brace balancing" / "parens balancing": Start from the outermost brace of the definition's body, count until you hit the last closing brace. 它没有给出关于何时“模板定义”实际上应该是模板定义的正式描述,但通常的实现是做“支撑平衡”/“平衡”的形式:从最外面的支撑开始定义的主体,直到你击中最后一个结束括号。 Everything in between is ignored. 中间的一切都被忽略了。

I suppose an example from the Standard clarifies this further 我想标准中的一个例子进一步说明了这一点

template<class T> class X {
  // ... (omitted) ...
  void g(T t) {
    +; // may be diagnosed even if X::g is not instantiated
  }
};

So early diagnosis of syntax or semantic errors in template definitions is a "quality of implementations". 因此,模板定义中语法或语义错误的早期诊断是“实现的质量”。


Normative description 规范性描述

These rules are of the quality "no diagnostic required". 这些规则具有“无需诊断”的质量。 Noteworthy, implementations by these rule are granted the license not to diagnose ill-formed template definitions even if the template is instantiated , despite what the non-normative note of the Standard says. 值得注意的是, 即使模板被实例化 ,这些规则的实现也被授予不诊断格式错误的模板定义的许可,尽管标准的非规范性注释说明了这一点。 If any rule for which no diagnostic is required is violated, an implementation is free to do whatever it wants with the entire program. 如果违反了任何不需要诊断的规则,则实现可以随意对整个程序执行任何操作。

It should be noted that also, there is no syntactically ill-formed "template definition", because this very term is defined by the syntax itself. 应该注意的是,也没有语法错误的“模板定义”,因为这个术语是由语法本身定义的。 A lonely + renders the whole enclosing context to be some soup of nonsensical tokens. 一个孤独的+使整个封闭的背景成为一些无意义的令牌。

Last but not least, the committee knows about these "loopholes", but there was no majority to change this so far, as far as I know. 最后但并非最不重要的是,委员会知道这些“漏洞”,但据我所知,迄今为止没有多数人改变这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM