Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Definition of template must be imported from module before it is required

I'd like to export (C++20) an alias template. VC++ 2019 compiles the code. Clang reports an error. Which one is correct and why?

// file: m.cppm
export module m;

template<typename T> struct my_template {};

export template<typename T> using my_alias = my_template<T>;
// file: main.cpp
import m;
int main() { my_alias<int> v; }
main.cpp:2:28: error: definition of 'my_template' must be imported from module 'm' before it is required
int main() { my_alias<int> v; }
^
m.cppm:3:29: note: previous definition is here
template<typename T> struct my_template {};
like image 711
sms Avatar asked Oct 28 '22 11:10

sms


1 Answers

This program is valid according to the current C++20 draft (which differs significantly from the Modules TS): export influences name lookup, not any more abstract notion of “usability”. One could obtain access to the non-exported my_template by any of a variety of means, including template argument deduction from an object whose type is one of its specializations.

like image 115
Davis Herring Avatar answered Nov 13 '22 15:11

Davis Herring