Advertisement
alestane

Prompt manipulator

Mar 9th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <utility>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. template <typename T>
  6. class prompt: private std::pair<std::string, T&> {
  7. public:
  8.     prompt(const std::string& text, T& target): std::pair<std::string, T&> {text, target} {}
  9.     friend std::istream& operator >> (std::istream& source, prompt<T> target)
  10.     {
  11.         auto request = source.tie();
  12.         if (request) { (*request) << target.first; }
  13.         source >> target.second;
  14.         return source;
  15.     }
  16. };
  17.        
  18. int main(int argc, char* argv[])
  19. {
  20.     int x = 5;
  21.     std::cin >> prompt<int>("Enter a number: ", x);
  22.     std::cout << x << std::endl;
  23.     return 0;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement