Advertisement
SepandMeenu

Type-safety in C++

Jul 16th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main() {
  4.  
  5.     std::size_t a = 1;
  6.     int b = -a;  // obviously, b <= a
  7.    
  8.     std::cout << "a = " << a << ", b = " << b << std::endl;  
  9.    
  10.     std::cout << "try without cast) ";
  11.     if (b <= a)
  12.         std::cout << "b <= a" << std::endl;
  13.     else
  14.         std::cout << "b > a" << std::endl;
  15.  
  16.     std::cout << "try with cast) ";
  17.     if (b <= static_cast<int>(a))
  18.         std::cout << "b <= a" << std::endl;
  19.     else
  20.         std::cout << "b > a" << std::endl;
  21.  
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement