Created
May 4, 2015 23:18
-
-
Save kdungs/9a61c8e6aaeee01948f1 to your computer and use it in GitHub Desktop.
Code for blog post.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CXX=clang++ | |
CXXFLAGS=-O3 -Werror -Weverything -pedantic -Wno-c++98-compat -std=c++14 | |
all: test_mult | |
clean: | |
rm -f test_mult |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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