Visual Studio 2008在编译模板时不关心基类是否存在?

发布于 2024-10-20 08:56:06 字数 383 浏览 3 评论 0原文

VS 2008 处理带有继承的类模板的方式似乎与其他编译器有点不同。 下面的代码在 VS 2008 上编译没有任何错误(使用默认选项):

template <typename S, typename T>
class someclass : public non_existent_class
{
T operator() (S s) const {
    return T(s);
}
};

问题是,为什么?由于未定义标识符 non_existent_class,没有其他编译器能够做到这一点(尝试过 GCC 4.5.0、Intel、Online Comeau、VS 2005)。也许新的 C++0x 标准中的某些内容证明了这种行为的合理性?

It seems that VS 2008 handles class templates with inheritance a bit differently from the other compilers.
The following code compiles without any error on VS 2008 (with default options):

template <typename S, typename T>
class someclass : public non_existent_class
{
T operator() (S s) const {
    return T(s);
}
};

Question is, why? No other compiler has been able to do this (tried GCC 4.5.0, Intel, Online Comeau, VS 2005), because of undefined identifier non_existent_class. Maybe it is something in the new C++0x standard that justifies this behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雄赳赳气昂昂 2024-10-27 08:56:06

这很可能是由于 Visual Studio 在应该执行两阶段时执行单阶段名称查找这一事实的结果。非依赖名称“non_existent_class”应该在第一阶段失败,无论模板是否被实例化,都会在定义点发生。

这是一个错误,但除非微软从根本上改变他们的编译器与模板的工作方式,否则它永远不会被修复。

http://womble.decadent.org.uk/c++/template -faq.html#two-phase

http ://blog.llvm.org/2009/12/dreaded-two-phase-name-lookup.html

This is most likely a consequence of the fact that visual studio does single phase name lookup when it's supposed to do two phase. The non-dependent name "non_existent_class" should fail in the first phase, which happens at the point of definition whether or not the template is ever instantiated.

It's a bug, but it's one that is never, ever going to be fixed until MS fundamentally changes the way their compiler works wrt templates.

http://womble.decadent.org.uk/c++/template-faq.html#two-phase

http://blog.llvm.org/2009/12/dreaded-two-phase-name-lookup.html

许你一世情深 2024-10-27 08:56:06

Visual Studio 的编译器从来不以其标准合规性而闻名。这一定是另一个错误,如果你报告它,他们会告诉你他们有更重要的事情要做。不用说,一旦实例化该类,编译器就会发出错误。

是的,有些事情编译器在实例化之前无法诊断。这不在他们的计算范围内,编译器必须发出诊断。 C++0x 不会改变这个规则

Visual Studio's compiler was never famous for its standards-compliance. This must be yet another bug, which, in case you'll report it, they'll tell you they have more important stuff to do. Needless to say that once you instantiate the class, the compiler will issue the error.

Yes, there are things which the compiler cannot diagnose before instantiation. This isn't in their count, and the compiler must issue a diagnostic. C++0x doesn't change this rule

蹲在坟头点根烟 2024-10-27 08:56:06

当您说编译器接受该代码而其他编译器不接受时,您的意思是使用该代码还是实例化模板?

模板不会在遇到定义时编译,而是在实例化时编译。如果您没有实例化模板,则编译器可能会完全忽略整个模板定义(尽可能多地忽略,因为它需要能够解释足够的代码才能知道模板定义何时完成)。

然后在稍后阶段,如果模板实际上被实例化,并且假设模板没有专门化,则编译器会抱怨。请注意,如果您为特定类型提供专业化,则该专业化不需要从同一个不存在的类继承。

在这种特殊情况下,我倾向于认为这是由于 Microsoft 编译器对依赖名称进行了特殊(如非标准)处理所致。同样,您不需要在模板内的依赖名称前面指定 typename (考虑基类,或者使用此模板的类型参数的不同模板的实例化) ),编译器可能会考虑在实际实例化发生之前定义基类,并等待那一刻来执行验证。

When you say that the compiler accepts that code, and other compilers don't, do you mean with that piece of code or also instantiating the template?

Templates are not compiled when the definition is encountered, but rather when they are instantiated. If you are not instantiating the template, it might be the case that the compiler is completely ignoring the whole template definition (in as much as it can, as it needs to be able to interpret enough of the code to know when the template definition finishes).

Then at a later stage, if the template is actually instantiated, and assuming that there is no specialization for the template the compiler will complain. Note that if you provide an specialization for a particular type, that specialization does not need to inherit from the same non-existent class.

In this particular case, I am inclined to think that this is due to the special (as in non-standard) treatment that the Microsoft compiler does of dependent names. In the same way that you don't need to specify typename in front of a dependent name inside a template (think of a base class, or an instantiation of a different template with a type argument of this template), the compiler might be considering that the base class will be defined before the actual instantiation takes place, and wait for that moment to perform the verification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文