Advertisement
krzotki

template specialization, static_assert()

Feb 22nd, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "string"
  3. #include "iostream"
  4.  
  5. template<typename T>
  6. void Print()
  7. {
  8.     static_assert(false, "Nieobsługiwany typ");
  9. }
  10.  
  11. template<>
  12. void Print<int>()
  13. {
  14.     std::cout << sizeof(int)<< std::endl;
  15. }
  16.  
  17. template<>
  18. void Print<float>()
  19. {
  20.     std::cout << sizeof(float) << std::endl;
  21. }
  22.  
  23. int main()
  24. {
  25.     Print<int>();
  26.     Print<float>();
  27.     //Print<std::string>(); //----> błąd  "Nieobsługiwany typ" podczas kompilacji
  28.  
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement