为什么编译器尝试实例化一个我实际上没有在任何地方实例化的模板?

Why does the compiler try to instantiate a template that I don't actually instantiate anywhere?

本文关键字:实例化 任何地 实际上 编译器 一个 为什么      更新时间:2023-10-16

更新如下
以下是我在main.cpp中的全部代码:

template<class T>
struct other_traits;
template<class T>
struct some_traits{
    typedef decltype(&T::operator()) Fty;
    typedef typename other_traits<Fty>::type type;
};
int main(){
}

但是,当g++编译得很好时,我在Visual Studio 2010中遇到了以下错误:

src\main.cpp(9):错误C2146:语法错误:缺少";"在标识符"type"之前
--src\main.cpp(10):请参阅对正在编译的类模板实例化"some_traits<T>"的引用
src\main.cpp(9):错误C2868:"some_traits<T>::type":使用声明的语法非法;预期合格名称

(我喜欢最后一个,总重量。)

我可以把它看作VC10中的一个错误吗?或者有什么好的理由提前实例化吗?或者是decltype的错误导致编译器认为Fty不是依赖名称?


更新:我试图欺骗编译器,认为Fty是一个使用基类从继承的依赖名称

template<class T>
struct other_traits;
template<class R, class C>
struct other_traits<R (C::*)()>{
    typedef R type;
};
template<class Fty>
struct base_traits{
    typedef typename other_traits<Fty>::type type;
};
template<class T>
struct some_traits
    : public base_traits<decltype(&T::operator())>
{};

但编译器仍然试图当场实例化/编译所有内容,并抛出以下错误:

srcmain.cpp(13): error C2039: 'type' : is not a member of 'other_traits<T>'
          with
          [
              T=
          ]
          srcmain.cpp(19) : see reference to class template instantiation 'base_traits<Fty>' being compiled
          with
          [
              Fty=
          ]
          srcmain.cpp(19) : see reference to class template instantiation 'some_traits<T>' being compiled
srcmain.cpp(13): error C2146: syntax error : missing ';' before identifier 'type'
srcmain.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
srcmain.cpp(13): error C2602: 'base_traits<Fty>::type' is not a member of a base class of 'base_traits<Fty>'
          with
          [
              Fty=
          ]
          srcmain.cpp(13) : see declaration of 'base_traits<Fty>::type'
          with
          [
              Fty=
          ]
srcmain.cpp(13): error C2868: 'base_traits<Fty>::type' : illegal syntax for using-declaration; expected qualified-name
          with
          [
              Fty=
          ]

请注意,模板参数为。有什么想法吗?

这似乎是一个错误(如果没有下面提到的特殊标志集)。以下是Oracle网站上C++模板的摘录:

7.2.2

ISO C++标准允许开发人员编写模板类并非所有成员具有给定模板的合法论点只要违法成员未实例化这个程序仍然很完善。ISOC++标准库使用此技巧然而-template=wholeclass选项实例化所有成员,因此不能与此类模板一起使用类,当使用有问题的模板参数。

当编译器看到decltype

D_4

您在这里做了一个假设,这在技术上是不正确的。让我们先解决这个问题。

您假设语法错误意味着模板被实例化。这不是编译模板的方式。模板在实例化之前首先进行编译。这是查找非依赖名称的阶段。在这个阶段肯定会发现语法错误。例如,无论模板参数如何,任何必须是声明的内容都必须以;结尾。

现在正确的问题是编译器在考虑other_traits<T>的专业化时是否正确。当然,第9行没有这样的专业化,尽管稍后可能会有专业化。但实例化的相关点是什么呢?我必须查一下(对不起,AFS Away From Standardother_traits<T>将是第9行,然后没有专门化,other_traits<Fty>::type无效。如果other_traits<Fty>::type的实例化点与some_traits<T>的实例化点相同,即没有,那么other_traits<Fty>::type应该在第1阶段被接受。