Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- using namespace std;
- template <typename Signature>
- class OneTimer;
- template <typename ReturnType, typename ...Args>
- class OneTimer<ReturnType(Args...)> {
- bool usable;
- ReturnType(*p)(Args...);
- public:
- OneTimer(ReturnType(*func)(Args...)) : p(func), usable(true) { }
- ReturnType operator()(Args ...args) {
- if (usable) {
- usable = false;
- return p(args...);
- }
- throw std::runtime_error("Poshel nahuy");
- }
- };
- int random() {
- return 42;
- }
- int main() {
- OneTimer<int()> a = random;
- for (int i = 0; i < 10; ++i) {
- try {
- std::cout << a() << std::endl;
- } catch(std::runtime_error &e) {
- std::cerr << e.what() << std::endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement