Skip to content

Instantly share code, notes, and snippets.

@hgiesel
Last active June 29, 2016 00:24
Show Gist options
  • Save hgiesel/610231150fb6686a4e7a to your computer and use it in GitHub Desktop.
Save hgiesel/610231150fb6686a4e7a to your computer and use it in GitHub Desktop.
Examples of useful C++ snippets
#include <iostream>
#include <string>
class Yen {
int _amount;
public:
Yen(size_t amount): _amount(amount) { /* */ }
friend auto operator<< (std::ostream& os, const Yen& yen) -> std::ostream& {
os << static_cast<int>(yen.amount()) << " Yen";
return os;
}
virtual auto amount(void) const noexcept -> const int {
return this->_amount;
}
virtual void amount(const int amount) noexcept {
this->_amount = amount;
}
};
auto operator"" _Y(unsigned long long yen) -> const Yen {
return Yen{yen};
}
int main(void) {
Yen y{134};
std::cout << y;
std::cout << 123_Y;
}
#include <memory>
#include <iostream>
class Widget {
int _value;
std::shared_ptr<Widget> _successor;
public:
Widget(void): _value(0) { /* */ };
Widget(const Widget& rhs): _value(0) { /* */ };
Widget(Widget&& rhs): _value(0) { /* */ };
virtual ~Widget(void) { /* */ }
operator bool(void) const;
void successor(Widget& successor);
void pass_value(int a);
auto pass_value(void) const -> int;
void value(int a);
auto value(void) const -> int;
void swap(Widget& swapper);
void share(Widget& sharer);
void reset(void);
};
Widget::operator bool(void) const {
return !!_successor;
}
void Widget::successor(Widget& successor) {
this->_successor.reset(&successor);
}
void Widget::pass_value(int a) {
_successor->_value = a;
}
int Widget::pass_value(void) const {
return _successor->value();
}
void Widget::value(int a)
{
this->_value = a;
}
int Widget::value(void) const {
return this->_value;
}
void Widget::swap(Widget& swapper) {
this->_successor.swap(swapper._successor);
}
void Widget::share(Widget& sharer) {
sharer._successor = this->_successor;
}
void Widget::reset(void) {
this->_successor.reset();
}
void test(Widget& a, Widget& b, Widget& c) {
a.successor(*new Widget{});
a.pass_value(111);
a.share(b);
b.pass_value(222);
c.successor(*new Widget{});
c.pass_value(333);
std::cout << "Step 1 ------------" << '\n';
std::cout << "Widget a: " << a.pass_value() << std::endl;
std::cout << "Widget b: " << b.pass_value() << std::endl;
std::cout << "Widget c: " << c.pass_value() << std::endl;
std::cout << '\n';
b.swap(c);
std::cout << "Step 2 ------------" << '\n';
std::cout << "Widget a: " << a.pass_value() << std::endl;
std::cout << "Widget b: " << b.pass_value() << std::endl;
std::cout << "Widget c: " << c.pass_value() << std::endl;
std::cout << '\n';
c.reset();
}
int main(void) {
Widget a{}, b{}, c{};
test(a, b, c);
std::cout << !!a << '\n';
std::cout << !!b << '\n';
std::cout << !!c << '\n';
std::cout << "Step 1 ------------" << '\n';
if (a) std::cout << "Widget a: " << a.pass_value() << std::endl;
if (b) std::cout << "Widget b: " << b.pass_value() << std::endl;
if (c) std::cout << "Widget c: " << c.pass_value() << std::endl;
}
// Expected output:
// Step 1 ------------
// Widget a: 222
// Widget b: 222
// Widget c: 333
//
// Step 2 ------------
// Widget a: 222
// Widget b: 333
// Widget c: 222
//
// 1
// 1
// 0
// Step 1 ------------
// Widget a: 222
// Widget b: 333
#include <memory>
#include <iostream>
class Widget {
int _value;
std::unique_ptr<Widget> _successor = nullptr;
public:
Widget(void): _value(0) { /* */ };
Widget(const Widget& rhs): _value(0) { /* */ };
Widget(Widget&& rhs): _value(0) { /* */ };
virtual ~Widget(void) { /* */ }
operator bool(void) const;
void value(int a);
auto value(void) const -> int;
void successor(Widget& successor);
void swap(Widget& swapper);
void reset(void);
void pass_value(int a);
auto pass_value(void) const -> int;
};
Widget::operator bool(void) const {
return !!_successor;
}
void Widget::value(int a) {
this->_value = a;
}
int Widget::value(void) const {
return this->_value;
}
void Widget::successor(Widget* const& successor) {
this->_successor.reset(successor);
}
void Widget::swap(Widget& swapper) {
this->_successor.swap(swapper._successor);
}
void Widget::reset(void) {
this->_successor.reset(nullptr);
}
void Widget::pass_value(int a) {
_successor->_value = a;
}
int Widget::pass_value(void) const {
return _successor->value();
}
void test(Widget& a, Widget& b, Widget& c) {
Widget* z{ new Widget{}};
a.successor(z);
a.pass_value(123);
b.successor(new Widget{});
b.pass_value(456);
// uncomment to see the world burn
// z will be pointed-to by two object, a and b, this will cause the std::unique_ptr in a to dangle later on
// b.reset();
// b.pass_value(z);
c.successor(new Widget{});
c.pass_value(789);
std::cout << "Step 1 ------------" << '\n';
std::cout << "Widget a: " << a.pass_value() << std::endl;
std::cout << "Widget b: " << b.pass_value() << std::endl;
std::cout << "Widget c: " << c.pass_value() << std::endl;
std::cout << '\n';
a.swap(b);
b.swap(c);
std::cout << "Step 2 ------------" << '\n';
std::cout << "Widget a: " << a.pass_value() << std::endl;
std::cout << "Widget b: " << b.pass_value() << std::endl;
std::cout << "Widget c: " << c.pass_value() << std::endl;
std::cout << '\n';
a.pass_value(1111111);
c.reset();
}
int main(void) {
Widget a{}, b{}, c{};
test(a, b, c);
std::cout << "Step 3 ------------" << '\n';
if (a) std::cout << "Widget a: " << a.pass_value() << std::endl;
if (b) std::cout << "Widget b: " << b.pass_value() << std::endl;
if (c) std::cout << "Widget c: " << c.pass_value() << std::endl;
std::cout << '\n';
}
// Expected Output:
// Step 1 ------------
// Widget a: 123
// Widget b: 456
// Widget c: 789
//
// Step 2 ------------
// Widget a: 456
// Widget b: 789
// Widget c: 123
//
// Step 3 ------------
// Widget a: 1111111
// Widget b: 789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment