是否有必要用导出来注释导出实体的模板特化?

Is it necessary to annotate the template specializations of an exported entity with export?

…还是在模块接口单元声明它们就足够了?

test.cpp:

module;

#include <iostream>

export module M;

export
template<typename>
class T
{
public:
  void foo() const
    { std::cout << "T" << std::endl; }
};

// export <-- NOT exported
template<>
class T<int>
{
public:
  void foo() const
    { std::cout << "T<int>" << std::endl; }
};

main.cpp:

import M;

int main()
{
  T<int> x; 
  x.foo();
  return 0;
}

输出:

$ rm -f gcm.cache/* && $(HOME)/gcc-master/bin/g++ -O3 -fmodules-ts test.cpp main.cpp -o foo && ./foo
T<int>

$ $(HOME)/gcc-master/bin/g++ --version
g++ (GCC) 11.0.0 20210128 (experimental)

导出仅影响名称查找和链接。这些都与任何类型的模板专门化或实例化无关,因此它们永远不需要导出。