TmetaDetection.cpp
Go to the documentation of this file.
#include <utl/meta/meta.h>
#include <gtest/gtest.h>
#include <type_traits>
namespace TmetaDetection {
using namespace utl;
using namespace meta;
/*
* Types to behave like Fixtures
*/
struct Foo {};
struct Bar {};
template <typename T> struct A {
using type = T; // nested type
A(int i, double d) : i_(i), d_(d) {} // declare a non-trivial constructor
A& operator++() { ++i_; return *this; } // declare an operator
// A sfinae function
template <typename TT = T, typename = when<std::is_integral<TT>::type::value>>
TT sfun () { return TT{}; }
private:
int i_; double d_;
};
// A binary metafunction
template <typename T1, typename T2>
struct mFun {
using type = std::is_same <T1, T2>;
};
// detectors
template <typename T> using try_type = typename T::type;
template <typename T> using try_none = typename T::none;
template <typename T> using try_ctor1= decltype (T(std::declval<int>(), std::declval<double>()));
template <typename T> using try_ctor2= decltype (T(std::declval<int>()));
template <typename T> using try_ppT = decltype (++(std::declval<T>()));
template <typename T> using try_Tpp = decltype (std::declval<T>()++);
template <typename T> using try_sfun = decltype (std::declval<T>().sfun());
/*
* void_t
*/
TEST(TmetaDetection, VoidType) {
EXPECT_EQ(true, (std::is_same<void, void_t<int, long, void*, void, Foo, Bar>>()));
EXPECT_EQ(true, (std::is_same<void, void_t<>>()));
}
/*
* not a type
*/
TEST(TmetaDetection, NotAType) {
EXPECT_EQ(false, (std::is_default_constructible<nat_>()));
EXPECT_EQ(false, (std::is_destructible<nat_>()));
EXPECT_EQ(false, (std::is_copy_constructible<nat_>()));
EXPECT_EQ(false, (std::is_copy_assignable<nat_>()));
}
/*
* Idiom
*/
TEST(TmetaDetection, IsDetected) {
EXPECT_EQ (true, (std::is_same< true_, is_detected<try_type, A<int>> >()));
EXPECT_EQ (true, (std::is_same< false_, is_detected<try_none, A<int>> >()));
EXPECT_EQ (true, (std::is_same< true_, is_detected<try_ctor1, A<int>> >()));
EXPECT_EQ (true, (std::is_same< false_, is_detected<try_ctor2, A<int>> >()));
EXPECT_EQ (true, (std::is_same< true_, is_detected<try_ppT, A<int>> >()));
EXPECT_EQ (true, (std::is_same< false_, is_detected<try_Tpp, A<int>> >()));
EXPECT_EQ (true, (std::is_same< true_, is_detected<try_sfun, A<int>> >()));
EXPECT_EQ (true, (std::is_same< false_, is_detected<try_sfun, A<double>> >()));
EXPECT_EQ (true, (std::is_same< true_, is_detected<mFun, int, void> >()));
EXPECT_EQ (true, (std::is_same< false_, is_detected<mFun, char> >()));
EXPECT_EQ (true, (std::is_same< false_, is_detected<mFun, char, void, void> >()));
EXPECT_EQ (true, (is_detected_v<try_type, A<int>>));
EXPECT_EQ (false,(is_detected_v<try_none, A<int>>));
EXPECT_EQ (true, (is_detected_v<mFun, int, void>));
}
/*
* Idiom
*/
TEST(TmetaDetection, Toolkit) {
// detected_t
EXPECT_EQ (true, (std::is_same< int, detected_t<try_type, A<int>> >()));
EXPECT_EQ (true, (std::is_same< nat_, detected_t< try_none, A<int>> >()));
EXPECT_EQ (true, (std::is_same< A<int>, detected_t< try_ctor1, A<int>> >()));
EXPECT_EQ (true, (std::is_same< nat_, detected_t< try_ctor2, A<int>> >()));
EXPECT_EQ (true, (std::is_same< A<int>&,detected_t< try_ppT, A<int>> >()));
EXPECT_EQ (true, (std::is_same< nat_, detected_t< try_Tpp, A<int>> >()));
EXPECT_EQ (true, (std::is_same< int, detected_t< try_sfun, A<int>> >()));
EXPECT_EQ (true, (std::is_same< nat_, detected_t< try_sfun, A<double>> >()));
EXPECT_EQ (true, (std::is_same< nat_, detected_t<mFun, void> >()));
EXPECT_EQ (true, (std::is_same< mFun<int, int>,
detected_t<mFun, int, int> >()));
// detected_or_t
EXPECT_EQ (true, (std::is_same< int, detected_or_t<Foo, try_type, A<int>> >()));
EXPECT_EQ (true, (std::is_same< Foo, detected_or_t<Foo, try_none, A<int>> >()));
EXPECT_EQ (true, (std::is_same< A<int>, detected_or_t<void, try_ctor1, A<int>> >()));
EXPECT_EQ (true, (std::is_same< void, detected_or_t<void, try_ctor2, A<int>> >()));
EXPECT_EQ (true, (std::is_same< A<int>&,detected_or_t<nil_, try_ppT, A<int>> >()));
EXPECT_EQ (true, (std::is_same< nil_, detected_or_t<nil_, try_Tpp, A<int>> >()));
EXPECT_EQ (true, (std::is_same< int, detected_or_t<void*, try_sfun, A<int>> >()));
EXPECT_EQ (true, (std::is_same< void*, detected_or_t<void*, try_sfun, A<double>> >()));
EXPECT_EQ (true, (std::is_same< nil_, detected_or_t<nil_, mFun, int> >()));
EXPECT_EQ (true, (std::is_same< mFun<char, int>,
detected_or_t<void, mFun, char, int> >()));
// is_detected_exact
EXPECT_EQ (true, (std::is_same<true_, is_detected_exact< int, try_type, A<int>> >()));
EXPECT_EQ (true, (std::is_same<false_, is_detected_exact< int, try_none, A<int>> >()));
EXPECT_EQ (true, (std::is_same<true_, is_detected_exact< A<int>, try_ctor1, A<int>> >()));
EXPECT_EQ (true, (std::is_same<false_, is_detected_exact< A<int>, try_ctor2, A<int>> >()));
EXPECT_EQ (true, (std::is_same<true_, is_detected_exact< A<int>&,try_ppT, A<int>> >()));
EXPECT_EQ (true, (std::is_same<false_, is_detected_exact< A<int>&,try_Tpp, A<int>> >()));
EXPECT_EQ (true, (std::is_same<true_, is_detected_exact< int, try_sfun, A<int>> >()));
EXPECT_EQ (true, (std::is_same<false_, is_detected_exact< int, try_sfun, A<double>> >()));
EXPECT_EQ (true, (std::is_same<true_, is_detected_exact<mFun<char, int>, mFun, char, int> >()));
EXPECT_EQ (true, (std::is_same<false_, is_detected_exact<mFun<int, int>, mFun, int> >())); // it would be better to check against mFun<int>
EXPECT_EQ (true, (is_detected_exact_v< int, try_type, A<int>> ));
EXPECT_EQ (false,(is_detected_exact_v< int, try_none, A<int>> ));
// is_detected_convertible
EXPECT_EQ (true, (std::is_same<true_, is_detected_convertible< int, try_type, A<char>> >()));
EXPECT_EQ (true, (std::is_same<false_, is_detected_convertible< int, try_none, A<int>> >()));
EXPECT_EQ (true, (std::is_same<false_, is_detected_convertible< mFun<int, int>, mFun, char, int> >()));
EXPECT_EQ (true, (is_detected_convertible_v< int, try_type, A<char>> ));
EXPECT_EQ (false,(is_detected_convertible_v< int, try_none, A<int>> ));
}
}
TmetaDetection::A::operator++
A & operator++()
Definition: TmetaDetection.cpp:37
TmetaDetection::try_sfun
decltype(std::declval< T >().sfun()) try_sfun
Definition: TmetaDetection.cpp:59
meta.h
Include all meta library.
utl::meta::void_t
void void_t
void_t type alias
Definition: detection.h:55
utl::meta::is_detected_exact_v
constexpr bool is_detected_exact_v
evaluates to true if evaluation of Op<Args...> is Expected and to false if not
Definition: detection.h:204
utl::meta::true_
bool_< true > true_
The type used as a compile-time boolean with true value.
Definition: integral.h:68
TmetaDetection::mFun::type
std::is_same< T1, T2 > type
Definition: TmetaDetection.cpp:49
utl::meta::is_detected_convertible
eval< std::is_convertible< detected_t< Op, Args... >, To > > is_detected_convertible
Definition: detection.h:231
TmetaDetection::try_none
typename T::none try_none
Definition: TmetaDetection.cpp:54
gtest.h
TmetaDetection::A::type
T type
Definition: TmetaDetection.cpp:35
utl::meta::nat_
Definition: detection.h:63
utl::meta::nil_
Definition: integral.h:43
TmetaDetection::A::sfun
TT sfun()
Definition: TmetaDetection.cpp:41
TmetaDetection::TEST
TEST(TmetaDetection, VoidType)
Definition: TmetaDetection.cpp:64
TmetaDetection::try_Tpp
decltype(std::declval< T >()++) try_Tpp
Definition: TmetaDetection.cpp:58
utl::meta::false_
bool_< false > false_
The type used as a compile-time boolean with false value.
Definition: integral.h:69
TmetaDetection
Definition: TmetaDetection.cpp:24
utl::meta::is_detected_convertible_v
constexpr bool is_detected_convertible_v
Definition: detection.h:237
utl
STL&#39;s core language concepts.
Definition: _1wire.h:30
utl::meta::detected_t
eval< detail::detector< nat_, void, Op, Args... > > detected_t
Definition: detection.h:146
TmetaDetection::A
Definition: TmetaDetection.cpp:34
TmetaDetection::try_type
typename T::type try_type
Definition: TmetaDetection.cpp:53
TmetaDetection::try_ctor2
decltype(T(std::declval< int >())) try_ctor2
Definition: TmetaDetection.cpp:56
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:16643
TmetaDetection::mFun
Definition: TmetaDetection.cpp:48
TmetaDetection::A::A
A(int i, double d)
Definition: TmetaDetection.cpp:36
TmetaDetection::Foo
Definition: TmetaDetection.cpp:31
utl::meta::is_detected_exact
eval< same_< Expected, detected_t< Op, Args... > > > is_detected_exact
Definition: detection.h:199
TmetaDetection::Bar
Definition: TmetaDetection.cpp:32
utl::meta::is_detected
typename detail::detector< nat_, void, Op, Args... >::detected is_detected
Definition: detection.h:118
TmetaDetection::try_ppT
decltype(++(std::declval< T >())) try_ppT
Definition: TmetaDetection.cpp:57
utl::meta::is_detected_v
constexpr bool is_detected_v
Detection predicate.
Definition: detection.h:122
utl::meta::detected_or_t
eval< detail::detected_or< Default, Op, Args... > > detected_or_t
Definition: detection.h:172
TmetaDetection::try_ctor1
decltype(T(std::declval< int >(), std::declval< double >())) try_ctor1
Definition: TmetaDetection.cpp:55