【问题标题】:If a function definition has a parameter of class template type and didn't use it (its members) then is it instantiated?如果一个函数定义有一个类模板类型的参数并且没有使用它(它的成员),那么它是否被实例化?
【发布时间】:2021-10-20 21:25:09
【问题描述】:

从我在这里发布的关于when the template is instantiated? 的上一个示例中,我得到了答案,即只有在使用模板时,编译器才会实例化它。但是看看这个例子:

template <typename T>
struct Pow{
    T operator()(T const& x){ return x * x; }
};

extern template struct Pow<int>; // explicit instantiation declaration

struct Foo{
    Pow<int> pi{};
    void fn(Pow<int>);
};
void Foo::fn(Pow<int> pw){
   // std::cout << pw(7) << '\n';
}

void bar(Pow<int> pwi){
    // std::cout << pwi(10) << '\n';
}

int main(){
    Foo f;
}
  • 如您所见,我已经声明了一个显式模板实例化Pow&lt;int&gt;,但还没有定义它。该程序运行良好,不会抱怨缺少Pow&lt;int&gt; 的定义!

  • 在上一个主题中,如果我使用模板类型作为函数定义(而不是声明)的参数类型,那么模板会被实例化,但在这里你可以看到:成员函数Foo::fn(Pow&lt;int&gt;) 和定义了普通函数函数bar(Pow&lt;int&gt;),但编译器没有抱怨Pow&lt;int&gt;的定义?!!!

  • 如果我取消注释上述函数中的行,程序将无法编译。那么这是否意味着Pow&lt;int&gt; 在用作函数定义中的函数参数和Foo::Pow&lt;int&gt; pi{}; 中的成员数据时不会被实例化?

  • 我觉得很困惑:

    void f(Pow<int>); // function declaration: Pow<int> is not instantiated yet.
    void f2(Pow<int>){} // function definition: Pow<int> instantiated? 
    void f3(pow<int> Pwi){ std::cout << Pwi(10);} // function definition and usage of `Pow<int>`: Pow<int> instantiated?
    
  • 主要:

    Foo f; // f has pi of type Pow<int>. so Pow<int> is instantiated? 
    

【问题讨论】:

  • Foo 是一个非模板类,那么Foo&lt;int&gt; 是什么意思?
  • Foo&lt;int&gt; 是一个错字,我的意思是Pow&lt;int&gt;。
  • "并且没有抱怨缺少 Pow 的定义!" - 因为它没有丢失。您误解了显式实例化声明的作用并使用该切线运行。
  • @StoryTeller-UnslanderMonica:啊!我看到抱怨缺少定义的不是编译器,而是链接器。所以这是因为 extern 关键字与它的定义分开声明?

标签: c++ template-instantiation


【解决方案1】:

程序运行良好,不会抱怨缺少Pow&lt;int&gt; 的定义!

因为它没有丢失。两种形式的显式实例化(声明和定义)都会导致类模板的实例化。显式实例化定义会导致成员函数的实例化(通常仅在需要时才延迟实例化)。另一方面,显式实例化声明将抑制成员函数的隐式实例化。即使那个成员函数体是可见的!

这是一个为一组受限制的类型编写模板,同时隐藏其实现的工具。它允许这样做:

//pow.h

template <typename T>
struct Pow{
    T operator()(T const& x); // Just a declaration 
};

extern template struct Pow<int>; // The class is instantiated, the member is 
                                 // assumed to be explicitly instantiated elsewhere

//pow.cpp
#include "pow.h"

template <typename T>
T Pow<T>::operator()(T const& x) { return x * x; }

template struct Pow<int>; // Explicit instantiation. The member function is defined
                          // in **this** translation unit.

这正是您的程序无法链接的原因。成员operator() 永远不会在您的程序中的任何地方实例化。您可以通过在某处提供显式实例化定义来修复它。比如

template <typename T>
struct Pow{
    T operator()(T const& x){ return x * x; }
};

// ...

int main() {
// ...
}

template struct Pow<int>; // Now the member function is emitted

它的另一个用途是潜在地缩短编译时间。如果您有一个您知道经常针对特定类型集实例化的类模板,您可以帮助链接器。

// my_string.h

template<typename charT>
class my_string {
  // Everything, members and all
};

extern template class my_string<char>;
extern template class my_string<wchar_t>;

// my_string.cpp 

#include <my_string.h>

// This file can be part of a shared object that ships with your library
// The common declarations are here

template class my_string<char>;
template class my_string<wchar_t>;

现在,链接器不必对常用的my_string&lt;char&gt; 和my_string&lt;wchar_t&gt; 的隐式实例化产生的许多重复符号进行排序。

【讨论】:

  • 但是当我们定义一个显式的模板实例化时,所有的成员都会被实例化,即使是那些没有被使用的。对吗?
  • @ItachiUchiwa - 是的,它不再懒惰了。
【解决方案2】:

Pow&lt;int&gt; 已定义。它被定义为Pow&lt;T&gt; 和T=int。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-17
    • 2014-06-05
    • 2019-12-27
    • 2011-01-18
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多