简体   繁体   English

类模板实例化

[英]class template instantiation

I just read the wiki article about CRTP , and I'm a little confused about template instantiation. 我刚刚阅读了有关CRTP的wiki文章,我对模板实例化有点困惑。

According to the wiki, 根据维基,

member function bodies (definitions) are not instantiated until long after their declarations. 成员函数体(定义)直到它们的声明后很久才被实例化。

I don't quite understand what it means. 我不太明白这意味着什么。

Suppose I got a class template: 假设我有一个类模板:

template <typename T>
class A
{
    public:
        void foo(T t)
        {
            //...
        };
};

When I instantiate the class template A, does it instantiate the member function foo()? 当我实例化类模板A时,它是否实例化成员函数foo()?

For example: 例如:

//in .cpp file
int main()
{
    A<int> a; //question 1
              //class template is instantiated here, isn't it? 
              //What about foo(), is it instantiated too?

    a.foo(10); //question 2
               //according to the quotation, foo() will not be instantiated until it is used.
               //if so, foo() is instantiated right here, not in question 1, right?
}

You seem to be confusing one thing: 你好像混淆了一件事:

Instantiation happens during compilation , not during runtime. 实例化在编译期间发生,而不是在运行时期间发生。 Hence you can't say "on which line" a class template or a function template was instantiated. 因此,您不能说“在哪一行”实例化类模板或函数模板。

That said, you're right about the fact that member function templates aren't instantiated together with class templates. 也就是说,你对成员函数模板没有与类模板一起实例化是正确的。

You could observe it in such a case: You have the following files 你可以在这种情况下观察它:你有以下文件

  • template.h (defines class A and function A::foo) template.h(定义类A和函数A :: foo)
  • a.cpp (uses A) a.cpp(使用A)
  • b.cpp (uses A and A::foo) b.cpp(使用A和A :: foo)

Then during compilation of a.cpp, only A would be instantiated. 然后在编译a.cpp期间,只会实例化A。 However, during compilation of b.cpp, both would be instantiated. 但是,在编译b.cpp期间,两者都将被实例化。

Because of this, in case A::foo contained some semantically invalid code for a given set of template parameters, you would get compile errors in b.cpp, but not a.cpp. 因此,如果A :: foo包含一组给定模板参数的语义无效代码,则会在b.cpp中出现编译错误,但不会出现a.cpp。

I hope that clears things up! 我希望能搞清楚!

With class templates, the rule of thumb is that only those members are instantiated which are actually used. 对于类模板,经验法则是只实例化那些实际使用的成员。

If you want complete instantiation, C++ offers explicit instantiation (however, usually you don't; the fact that not every bit is fully instantiated means that your template class is even more generic as it lowers the requirements on T , note that syntax checking and lookup of non-dependent types (stuff that is not dependent on T ) still happens). 如果你想要完整的实例化,C ++提供了显式的实例化 (但是,通常你不会;事实上并非每个位都被完全实例化意味着你的模板类更通用,因为它降低了对T的要求,请注意语法检查和查找非依赖类型(不依赖于T东西)仍然会发生)。

You will find a more complete answer here: Template instantiation details of GCC and MS compilers 您将在此处找到更完整的答案: GCC和MS编译器的模板实例化详细信息

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

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