Skip to content

Instantly share code, notes, and snippets.

@dkorolev
Created April 8, 2015 15:30
Show Gist options
  • Save dkorolev/ce687e33b642e2de0ce3 to your computer and use it in GitHub Desktop.
Save dkorolev/ce687e33b642e2de0ce3 to your computer and use it in GitHub Desktop.
tempate-d using.
#include <iostream>
enum class Selector : int { A, B };
struct ImplA {
void foo() {
std::cout << "A::foo()" << std::endl;
}
int a;
constexpr static bool is_a = true;
constexpr static bool is_b = false;
};
struct ImplB {
void foo() {
std::cout << "B::foo()" << std::endl;
}
int b;
constexpr static bool is_a = false;
constexpr static bool is_b = true;
};
template<Selector> struct ImplAB {
};
template<> struct ImplAB<Selector::A> {
typedef ImplA type;
};
template<> struct ImplAB<Selector::B> {
typedef ImplB type;
};
template<Selector T = Selector::A> using Impl = typename ImplAB<T>::type;
int main() {
Impl<Selector::A> a;
a.foo();
a.a = 42;
static_assert(a.is_a, "");
static_assert(!a.is_b, "");
Impl<Selector::B> b;
b.foo();
b.b = 100;
static_assert(!b.is_a, "");
static_assert(b.is_b, "");
Impl<> d;
d.foo();
static_assert(d.is_a, "");
static_assert(!d.is_b, "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment