Advertisement
Radfler

::scan

Dec 29th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #ifdef __cpp_concepts
  4. # define SCAN_REQUIRES(S, T) requires requires(T t) { S >> t; }
  5. #else
  6. # define SCAN_REQUIRES(S, T)
  7. #endif
  8.  
  9. namespace impl {
  10.  
  11.     template<typename CharT>
  12.     struct scan_fn;
  13.  
  14.     template<>
  15.     struct scan_fn<char> {
  16.         template<typename T> SCAN_REQUIRES(std::cin, T)
  17.         operator T() const {
  18.             T t;
  19.             std::cin >> t;
  20.             return t;
  21.         }
  22.     };
  23.  
  24.     template<>
  25.     struct scan_fn<wchar_t> {
  26.         template<typename T> SCAN_REQUIRES(std::wcin, T)
  27.         operator T() const {
  28.             T t;
  29.             std::wcin >> t;
  30.             return t;
  31.         }
  32.     };
  33.  
  34. }
  35.  
  36. inline constexpr impl::scan_fn<char> scan;
  37. inline constexpr impl::scan_fn<wchar_t> wscan;
  38.  
  39. #undef SCAN_REQUIRES
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement