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

Viewed 196

… or is it enough to declare them at the module interface unit?

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;
}

Output:

$ 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)
1 Answers

Exporting affects only name lookup and linkage. Neither of those is relevant to any kind of template specialization or instantiation, so they never need exporting.

Related