Add traits for exploiting SFINAE
This commit is contained in:
parent
722e9ae590
commit
bd9cdfccb2
|
@ -28,6 +28,7 @@ set(HEADER_FILES
|
||||||
math/math.h
|
math/math.h
|
||||||
misc/memory.h
|
misc/memory.h
|
||||||
misc/random.h
|
misc/random.h
|
||||||
|
misc/traits.h
|
||||||
tests/testutils.h
|
tests/testutils.h
|
||||||
tests/cppunit.h
|
tests/cppunit.h
|
||||||
)
|
)
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue