eATM

c++模板元编程,判断类型是否支持序列化,判断类型是否重载了等于符号

				
						//判断类型是否支持序列化
	template <typename T>
	struct is_serializable
	{
	private:
		// 去除指针类型
		using base_type = std::remove_pointer_t<T>;

	public:
		template <typename U>
		static auto test(int) -> decltype(std::declval<std::ostream&>() << std::declval<U>(), std::true_type());

		template <typename>
		static auto test(...) -> std::false_type;

		static constexpr bool value = decltype(test<base_type>(0))::value;
	};

	//判断类型是否支持重载了等于==符号
	template <typename T>
	struct is_equality_comparable
	{
	private:
		// 去除引用和const限定符
		using base_type = std::remove_cv_t<std::remove_reference_t<T>>;

		template <typename U>
		static auto test(int) -> decltype(std::declval<const U&>() == std::declval<const U&>(), std::true_type());

		template <typename>
		static auto test(...) -> std::false_type;

	public:
		static constexpr bool value = decltype(test<base_type>(0))::value;
	};
				
			

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注