fork download
  1. #include <iostream>
  2.  
  3. template<typename T>
  4. struct traits1{
  5. static T add(T a, T b) { return a+b; } /* default */
  6. static T mul(T a, T b); /* no default */
  7. };
  8.  
  9.  
  10. template<typename T>
  11. struct traits2{
  12. static T add(T a, T b);
  13. static T mul(T a, T b);
  14. };
  15.  
  16. template<>
  17. int traits2<int>::add(int a, int b) { return a*b; }
  18.  
  19. int main()
  20. {
  21. std::cout << traits1<int>::add(40, 2) << "\n";
  22.  
  23. // error: mul is not a member of traits1<int>
  24. std::cout << traits1<int>::mul(40, 2) << "\n";
  25.  
  26. std::cout << traits2<int>::add(40, 2) << "\n";
  27. // error: undefined reference to traits2<int>::mul(int, int)
  28. std::cout << traits2<int>::mul(40, 2) << "\n";
  29. return 0;
  30. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/XWFTpj/cckiScdw.o: In function `main':
prog.cpp:(.text+0x8a): undefined reference to `traits1<int>::mul(int, int)'
prog.cpp:(.text+0xf2): undefined reference to `traits2<int>::mul(int, int)'
collect2: ld returned 1 exit status
stdout

Standard output is empty