fork download
  1. #ifndef TYPETOKEN_HPP_
  2. #define TYPETOKEN_HPP_
  3.  
  4. #include <atomic>
  5.  
  6. namespace typetoken
  7. {
  8.  
  9. using token_t = std::size_t;
  10. template<typename T> token_t getToken(void);
  11.  
  12. class tokenbase {
  13. private:
  14. tokenbase(void) = delete;
  15. static volatile std::atomic<token_t> counter;
  16.  
  17. template<typename Q>
  18. friend token_t getToken(void);
  19. };
  20.  
  21. template<typename T>
  22. struct typetoken : tokenbase {
  23. private:
  24. typetoken(void) = delete;
  25. static token_t ID;
  26.  
  27. template<typename Q>
  28. friend token_t getToken(void);
  29. };
  30.  
  31. template<typename T>
  32. token_t typetoken<T>::ID = 0;
  33.  
  34. template<typename T>
  35. token_t getToken(void) {
  36. typedef typetoken<T> token;
  37. if (!token::ID) {
  38. token::ID = ++token::counter;
  39. }
  40. return token::ID;
  41. }
  42. } // namespace typetoken
  43.  
  44. #endif /* TYPETOKEN_HPP_ */
  45.  
  46. #include <iostream>
  47.  
  48. int main(){
  49. std::cout << typetoken::getToken<int>() << ' ' << typetoken::getToken<signed int>() << ' ' << typetoken::getToken<char>() << ' ' << typetoken::getToken<int>() << '\n';
  50. }
  51.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/osozHv/ccG7l86y.o: In function `unsigned int typetoken::getToken<int>()':
prog.cpp:(.text._ZN9typetoken8getTokenIiEEjv[_ZN9typetoken8getTokenIiEEjv]+0x16): undefined reference to `typetoken::tokenbase::counter'
/home/osozHv/ccG7l86y.o: In function `main':
prog.cpp:(.text.startup+0x25): undefined reference to `typetoken::tokenbase::counter'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty