Skip to content

Instantly share code, notes, and snippets.

@kdungs
Created May 4, 2015 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdungs/9a61c8e6aaeee01948f1 to your computer and use it in GitHub Desktop.
Save kdungs/9a61c8e6aaeee01948f1 to your computer and use it in GitHub Desktop.
Code for blog post.
CXX=clang++
CXXFLAGS=-O3 -Werror -Weverything -pedantic -Wno-c++98-compat -std=c++14
all: test_mult
clean:
rm -f test_mult
#ifndef MULT_H
#define MULT_H
struct A { using type = A; };
struct B { using type = B; };
template <typename... Args>
struct Mult;
template <typename LHS, typename... RHS>
struct Mult<LHS, RHS...> {
using type =
typename Mult<typename LHS::type, typename Mult<RHS...>::type>::type;
};
template <typename LHS, typename RHS>
struct Mult<LHS, RHS> {
using type = typename Mult<typename LHS::type, typename RHS::type>::type;
};
template <>
struct Mult<A, A> {
using type = A;
};
template <>
struct Mult<B, B> {
using type = A;
};
template <>
struct Mult<A, B> {
using type = B;
};
template <>
struct Mult<B, A> {
using type = B;
};
#endif // MULT_H
#include "mult.h"
#include <iostream>
#include <typeinfo>
int main() {
std::cout << typeid(Mult<B, A, B, B, B, A>::type).name() << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment