#include <iostream>
#include <type_traits>
#include <string>
template <class T, class Enable = void>
class Foo {};
template<class T>
class Foo<T, typename std::enable_if<std::is_integral<T>::value>::type> {
public:
std::string wow() {
return "WOW";
}
};
template<class T>
class Foo<T, typename std::enable_if<std::is_class<T>::value>::type> {
public:
std::string wow() {
return "Meo";
}
};
class Bar {};
int main() {
std::cout << Foo<int>().wow() << '\n';
std::cout << Foo<long>().wow() << '\n';
std::cout << Foo<Bar>().wow();
return 0;
}
WOW
WOW
Meo
mt19937 gen(time(0));
uniform_int_distribution<int> uid(1, 9);
int f(int n) {
if (n >= 3)
return n + 1;
else
return n;
}
int i = f(uid(gen));
#include <iostream>
#include <stack>
#include <memory>
template<typename T>
class Undo : public T {
private:
std::shared_ptr<std::stack<Undo>> history_ptr = std::make_shared<std::stack<Undo>>();
public:
template <typename... Args>
explicit Undo(const Args&... args): T(args ...) {}
void save() {
history_ptr->push(*this);
}
void undo() {
if (!history_ptr->empty()) {
*this = history_ptr->top();
history_ptr->pop();
}
}
};
class Foo {
public:
int a = 0;
std::string s;
Foo(const int _a, std::string _s): a(_a), s(std::move(_s)) {}
};
std::ostream &operator<<(std::ostream &out, const Foo &foo) {
out << "Foo(" << foo.a << ", \"" << foo.s << "\")";
return out;
}
int main() {
Undo<Foo> foo(1, "Hello World!");
foo.save();
std::cout << "Save\t" << foo << std::endl;
foo.a = 6661313;
foo.save();
std::cout << "Save\t" << foo << std::endl;
foo.s = "Lakad Matataaag!";
foo.save();
std::cout << "Save\t" << foo << std::endl;
foo.a = 9000;
foo.s = "Normalin, Normalin!";
foo.save();
std::cout << "Save\t" << foo << std::endl;
foo.a = 12345678;
foo.s = "Da next lebel play!";
std::cout << "Current\t" << foo << std::endl;
foo.undo();
std::cout << "Undo\t" << foo << std::endl;
foo.undo();
std::cout << "Undo\t" << foo << std::endl;
foo.undo();
std::cout << "Undo\t" << foo << std::endl;
foo.undo();
std::cout << "Undo\t" << foo << std::endl;
return 0;
}