#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