fork download
  1. /* a.h */
  2. #pragma once
  3.  
  4. template<typename A> class Foo;
  5. template<typename A> A func(Foo<A> const &f);
  6.  
  7. template<typename A>
  8. class Foo {
  9. public:
  10. Foo(A a) : _a(a) {}
  11.  
  12. private:
  13. A _a;
  14.  
  15. friend A func<A>(Foo const &f);
  16. };
  17.  
  18. /* a.cpp */
  19. #include "a.h"
  20.  
  21. template class Foo<int>;
  22. template int func<int>(Foo<int> const&);
  23.  
  24. template <typename A>
  25. A func(Foo<A> const &f) {
  26. return f._a;
  27. }
  28.  
  29. /* main.cpp */
  30. #include <iostream>
  31. #include "a.h"
  32.  
  33. int main() {
  34. Foo<int> f1(10), f2(20);
  35. int a = func(f1);
  36. std::cout << a << '\n' << func(f2) << '\n';
  37. }
  38.  
  39. /* g++ a.cpp main.cpp */
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:9: warning: #pragma once in main file [enabled by default]
 #pragma once
         ^
prog.cpp:19:15: fatal error: a.h: No such file or directory
 #include "a.h"
               ^
compilation terminated.
stdout
Standard output is empty