C++11:type_traits (3) type prope

2020-02-06  本文已影响0人  fck_13
template<class T> struct is_const          : std::false_type {};
template<class T> struct is_const<const T> : std::true_type {};

代码示例如下:

EXPECT_FALSE(std::is_const<int>::value); // false
EXPECT_TRUE(std::is_const<const int>::value); // true
EXPECT_FALSE(std::is_const<const int*>::value); // false
EXPECT_TRUE(std::is_const<int* const>::value); // true
EXPECT_FALSE(std::is_const<const int&>::value); // false
EXPECT_TRUE(std::is_const<typename std::remove_reference<const int&>::type>::value); // true
template<class T> struct is_volatile             : std::false_type {};
template<class T> struct is_volatile<volatile T> : std::true_type {};

代码示例如下:

EXPECT_FALSE(std::is_volatile<int>::value);
EXPECT_TRUE(std::is_volatile<volatile int>::value);
template< class T >
struct is_trivial : std::integral_constant< 
    bool,
    std::is_trivially_copyable<T>::value &&
    std::is_trivially_default_constructible<T>::value 
> {};

代码示例如下:

struct A 
{
    int m;
};

struct B 
{
    B() {}
};
EXPECT_TRUE(std::is_trivial<A>::value);
EXPECT_FALSE(std::is_trivial<B>::value);

代码示例如下:

struct A 
{
    int m;
};

struct B 
{
    B(B const&) {}
};

struct C 
{
    virtual void foo()
};

struct D 
{
    int m;

    D(D const&) = default; // -> trivially copyable
    D(int x) : m(x + 1) {}
};
    
EXPECT_TRUE(std::is_trivially_copyable<A>::value);
EXPECT_FALSE(std::is_trivially_copyable<B>::value);
EXPECT_FALSE(std::is_trivially_copyable<C>::value);
EXPECT_TRUE(std::is_trivially_copyable<D>::value);
struct A 
{
    int m;
};

struct B 
{
    int m1;
private:
    int m2;
};

struct C 
{
    virtual void foo()
}
};

EXPECT_TRUE(std::is_standard_layout<A>::value);
EXPECT_FALSE(std::is_standard_layout<B>::value);
EXPECT_FALSE(std::is_standard_layout<C>::value);
struct A
{
    int m;
};

struct B
{
    int m1;
private:
    int m2;
};

struct C 
{
    virtual void foo()
}
};

EXPECT_TRUE(std::is_pod<A>::value);
EXPECT_FALSE(std::is_pod<B>::value);
EXPECT_FALSE(std::is_pod<C>::value);
struct A 
{
    int m;
};

struct B 
{
    virtual ~B();
};
    
EXPECT_TRUE(std::is_literal_type<A>::value);
EXPECT_FALSE(std::is_literal_type<B>::value);
struct A {};

struct B 
{
    int m;
};

struct C 
{
    static int m;
};

struct D 
{
    virtual ~D();
};

union E {};

struct F 
{
    [[no_unique_address]] E e;
};

EXPECT_TRUE(std::is_empty<A>::value);
EXPECT_FALSE(std::is_empty<B>::value);
EXPECT_TRUE(std::is_empty<empty::C>::value);
EXPECT_FALSE(std::is_empty<D>::value);
EXPECT_FALSE(std::is_empty<E>::value);
EXPECT_FALSE(std::is_empty<F>::value);

可能的实现:

namespace detail {
 
template <class T>
std::true_type detect_is_polymorphic(
    decltype(dynamic_cast<const volatile void*>(static_cast<T*>(nullptr)))
);
template <class T>
std::false_type detect_is_polymorphic(...);
 
} // namespace detail
 
template <class T>
struct is_polymorphic : decltype(detail::detect_is_polymorphic<T>(nullptr)) {};

代码示例如下:

struct A 
{
    int m;
};

struct B 
{
    virtual void foo();
};

struct C : B {};

EXPECT_FALSE(std::is_polymorphic<A>::value);
EXPECT_TRUE(std::is_polymorphic<B>::value);
EXPECT_TRUE(std::is_polymorphic<C>::value);
struct A 
{
    int m;
};

struct B 
{
    virtual void foo();
};

struct C 
{
    virtual void foo() = 0;
};

struct D : C {};

EXPECT_FALSE(std::is_abstract<A>::value);
EXPECT_FALSE(std::is_abstract<B>::value);
EXPECT_TRUE(std::is_abstract<C>::value);
EXPECT_TRUE(std::is_abstract<D>::value);
namespace detail {
template<typename T,bool = std::is_arithmetic<T>::value>
struct is_signed : std::integral_constant<bool, T(-1) < T(0)> {};
 
template<typename T>
struct is_signed<T,false> : std::false_type {};
} // namespace detail
 
template<typename T>
struct is_signed : detail::is_signed<T>::type {};
class A {};
enum B : int {};
enum class C : int {};

EXPECT_FALSE(std::is_signed<A>::value);
EXPECT_TRUE(std::is_signed<float>::value);
EXPECT_TRUE(std::is_signed<signed int>::value);
EXPECT_FALSE(std::is_signed<unsigned int>::value);
EXPECT_FALSE(std::is_signed<B>::value);
EXPECT_FALSE(std::is_signed<C>::value);
EXPECT_TRUE(std::is_signed<signed int>());
EXPECT_FALSE(std::is_signed<unsigned int>());
namespace detail {
template<typename T,bool = std::is_arithmetic<T>::value>
struct is_unsigned : std::integral_constant<bool, T(0) < T(-1)> {};
 
template<typename T>
struct is_unsigned<T,false> : std::false_type {};
} // namespace detail
 
template<typename T>
struct is_unsigned : detail::is_unsigned<T>::type {};

代码示例如下:

class A {};
enum B : unsigned {};
enum class C : unsigned {};

EXPECT_FALSE(std::is_unsigned<A>::value);
EXPECT_FALSE(std::is_unsigned<float>::value);
EXPECT_FALSE(std::is_unsigned<signed int>::value);
EXPECT_TRUE(std::is_unsigned<unsigned int>::value);
EXPECT_FALSE(std::is_unsigned<B>::value);
EXPECT_FALSE(std::is_unsigned<C>::value);

(未完待续)

上一篇 下一篇

猜你喜欢

热点阅读