r/badcode •

[deleted by user]

[deleted] — view deleted post

0 Upvotes

35 comments sorted by

View all comments

1

u/fb39ca4 depraved Oct 27 '19

Here's another one that is hybrid bash and C++ template metaprogramming:

//usr/bin/c++ "$0" "-DINPUT=`echo $@ | tr ' ' ','`" && echo "your bones seem to be in order"; exit

#include <type_traits>

namespace dem_bones {

enum bone {
	head,
	neck,
	shoulder,
	back,
	hip,
	thigh,
	knee,
	shin,
	ankle,
	heel,
	foot,
	toe,
	num_bones
};

namespace detail {
	template<bone, bone>
	struct connected : std::false_type {};

	template<> struct connected<bone::toe,      bone::foot>     : std::true_type {};
	template<> struct connected<bone::foot,     bone::heel>     : std::true_type {};
	template<> struct connected<bone::heel,     bone::ankle>    : std::true_type {};
	template<> struct connected<bone::ankle,    bone::shin>     : std::true_type {};
	template<> struct connected<bone::shin,     bone::knee>     : std::true_type {};
	template<> struct connected<bone::knee,     bone::thigh>    : std::true_type {};
	template<> struct connected<bone::thigh,    bone::hip>      : std::true_type {};
	template<> struct connected<bone::hip,      bone::back>     : std::true_type {};
	template<> struct connected<bone::back,     bone::shoulder> : std::true_type {};
	template<> struct connected<bone::shoulder, bone::neck>     : std::true_type {};
	template<> struct connected<bone::neck,     bone::head>     : std::true_type {};

	template<int N, bone... B>
	struct bone_list {
		static constexpr bool sorted = N == static_cast<int>(bone::num_bones);
	};

	template<int N, bone B>
	struct bone_list<N, B> {
		static constexpr bool sorted = bone_list<N + 1>::sorted;
	};

	template<int N, bone B0, bone B1, bone... B>
	struct bone_list<N, B0, B1, B...> {
		static constexpr bool sorted = connected<B0, B1>{} && bone_list<N + 1, B1, B...>::sorted;
	};
}

template<bone...B>
constexpr bool bones_sorted = detail::bone_list<0, B...>::sorted;

static_assert(bones_sorted<INPUT>, "Oh no you have missing and/or misordered bones!");

}