alestane icon

Prompt manipulator

alestane | PRO | 03/09/18 11:36:43 PM UTC | 0 ⭐ | 355 👁️ | Never ⏰ | []
C++ |

650 B

|

None

|

0 👍

/

0 👎

#include <utility>
#include <string>
#include <iostream>
 
template <typename T>
class prompt: private std::pair<std::string, T&> {
public:
    prompt(const std::string& text, T& target): std::pair<std::string, T&> {text, target} {}
    friend std::istream& operator >> (std::istream& source, prompt<T> target)
    {
        auto request = source.tie();
        if (request) { (*request) << target.first; }
        source >> target.second;
        return source;
    }
};
        
int main(int argc, char* argv[])
{
    int x = 5;
    std::cin >> prompt<int>("Enter a number: ", x);
    std::cout << x << std::endl;
    return 0;
}

Comments