-
std::is_const
: 判断一个类型是否有const
限定。
可能的实现为:
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
-
std::is_volatile
: 判断一个类型是否为volatile
限定。
可能的实现为:
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);
-
std::is_trivial
: 判断一个类型是否是trivial
。
trivial type:或者是scalar type,或者是有trivial 默认构造函数的平凡可拷贝的类,或者是上述类型的数组。这些类型可能是有const
和volatile
限定的。
可能的实现为:
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);
-
std::is_trivially_copyable
: 判断一个类型是否有trivil拷贝函数
可能的实现为:
代码示例如下:
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);
-
std::is_standard_layout
: 判断一个类型是否为标准布局类型
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);
-
std::is_pod
: 判断一个类型是否为POD类型
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);
-
std::is_literal_type
: checks if a type is a literal type
struct A
{
int m;
};
struct B
{
virtual ~B();
};
EXPECT_TRUE(std::is_literal_type<A>::value);
EXPECT_FALSE(std::is_literal_type<B>::value);
-
std::is_empty
: 判断一个类(非联合)是否为没有静态数据成员。
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);
-
std::is_polymorphic
: 判断一个类型是否为多态类型
可能的实现:
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);
-
std::is_abstract
: 判断一个类型是否为抽象类型
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);
-
std::is_signed
: 判断一个类型是否为signed
的算数类型。
可能的实现为:
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>());
-
std::is_unsigned
: 判断一个类型是否为unsigned
算数类型。
可能的实现:
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);
(未完待续)