Add traits for exploiting SFINAE

This commit is contained in:
Martchus 2016-11-13 22:51:09 +01:00
parent 722e9ae590
commit bd9cdfccb2
2 changed files with 40 additions and 0 deletions

View File

@ -28,6 +28,7 @@ set(HEADER_FILES
math/math.h
misc/memory.h
misc/random.h
misc/traits.h
tests/testutils.h
tests/cppunit.h
)

39
misc/traits.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef CPP_UTILITIES_TRAITS_H
#define CPP_UTILITIES_TRAITS_H
#include <type_traits>
namespace Traits {
namespace Detail {
enum class Enabler {};
}
template <typename If, typename Then, typename Else>
using Conditional = typename std::conditional<If::value, Then, Else>::type;
template <bool B, typename...>
struct Bool : std::integral_constant<bool, B> {};
template <typename T>
using Not = Bool<!T::value>;
template <typename... T>
struct Any : Bool<false> {};
template <typename... T>
struct All : Bool<true> {};
template <typename Head, typename... Tail>
struct All<Head, Tail...> : Conditional<Head, All<Tail...>, Bool<false> > {};
template <typename... Condition>
using EnableIf = typename std::enable_if<All<Condition...>::value, Detail::Enabler>::type;
template <typename... Condition>
using DisableIf = typename std::enable_if<!All<Condition...>::value, Detail::Enabler>::type;
}
#endif // CPP_UTILITIES_TRAITS_H