Skip to content

Instantly share code, notes, and snippets.

@pniedzielski
Last active November 3, 2020 07:11
Show Gist options
  • Save pniedzielski/d4d02bdb91ffbc0436a2 to your computer and use it in GitHub Desktop.
Save pniedzielski/d4d02bdb91ffbc0436a2 to your computer and use it in GitHub Desktop.
Strong typedef / member functions on built-in types (C++17)
// Transcribed (with simple modifications and added comments) from _Fundamentals of Type-Dependent Code Reuse in C++_ presentation by Mark Isaacson at NDC 2015
template <typename T, template <typename> class... Mixins>
struct Mixer : public Mixins< Mixer<T, Mixins...> >... {
// N4477 Operator Dot (R2), on track for C++17
T& operator.() { return value; }
private:
T value;
};
template <typename T>
struct HelloMixin {
void print_hello() {
using namespace std;
cout << "hello\n"s;
}
};
using helloable_int = Mixer<int, HelloMixin>;
// …
auto x = helloable_int{};
auto y = x + 5; // ← like an int here
x.print_hello(); // ← voodoo here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment