stl模板库成员函数重载类型混肴编译不通过解决方法

2023-12-13 11:44:24

stl模板库成员函数重载类型混肴编译不通过解决方法
这种方式编译不通过IsArithmetic和HasMemberList编译器存在混肴

template <typename T, typename Enable = std::enable_if<IsArithmetic<T>::value>::type >
		static void DumpWrapper(T* filed, std::string fieldname)
		{
			printf("fieldname:=>%s\r\n", fieldname.c_str());
			enum : std::size_t { IntValSize = sizeof(T) };
			if (IntValSize > 4)
			{
				printf("%s:=>%016llx\r\n", fieldname.c_str(), *filed);
			}
			else
			{
				printf("%s:=>%08x\r\n", fieldname.c_str(), *filed);
			}
		}
		template <typename T, typename  Enable = std::enable_if<IsArithmetic<T>::value >::type  >
		static void DumpWrapper(T filed, std::string fieldname)
		{
			printf("fieldname:=>%s\r\n", fieldname.c_str());
			enum : std::size_t { IntValSize = sizeof(T) };
			if (IntValSize > 4)
			{
				printf("%s:=>%016llx\r\n", fieldname.c_str(), filed);
			}
			else
			{
				printf("%s:=>%08x\r\n", fieldname.c_str(), filed);
			}
		}


		template <typename T, typename Enable = EnableIfHasMemberList<T>>
		static void DumpWrapper(nop::S7PlusPacketValue<T>* filed, std::string fieldname)
		{
			printf("fieldname:=>%s\r\n", fieldname.c_str());
			filed.Dump();

		}
		template <typename T, typename Enable = EnableIfHasMemberList<T>>
		static void DumpWrapper(nop::S7PlusPacketValue<T> filed, std::string fieldname)
		{
			printf("fieldname:=>%s\r\n", fieldname.c_str());
			filed->Dump();

		}

解决方法必须明确重载IsArithmetic和HasMemberList编译器存在混肴,加上双重判断typename Enable = std::enable_if<IsArithmetic::value&!HasMemberList::value>::type来明确混肴条件,这种方式编译通过


		template <typename T, typename Enable = std::enable_if<IsArithmetic<T>::value&!HasMemberList<T>::value>::type >
		static void DumpWrapper(T* filed, std::string fieldname)
		{
			printf("fieldname:=>%s\r\n", fieldname.c_str());
			enum : std::size_t { IntValSize = sizeof(T) };
			if (IntValSize > 4)
			{
				printf("%s:=>%016llx\r\n", fieldname.c_str(), *filed);
			}
			else
			{
				printf("%s:=>%08x\r\n", fieldname.c_str(), *filed);
			}
		}
		template <typename T, typename  Enable = std::enable_if<IsArithmetic<T>::value & !HasMemberList<T>::value>::type  >
		static void DumpWrapper(T filed, std::string fieldname)
		{
			printf("fieldname:=>%s\r\n", fieldname.c_str());
			enum : std::size_t { IntValSize = sizeof(T) };
			if (IntValSize > 4)
			{
				printf("%s:=>%016llx\r\n", fieldname.c_str(), filed);
			}
			else
			{
				printf("%s:=>%08x\r\n", fieldname.c_str(), filed);
			}
		}


		template <typename T, typename Enable = EnableIfHasMemberList<T>>
		static void DumpWrapper(nop::S7PlusPacketValue<T>* filed, std::string fieldname)
		{
			printf("fieldname:=>%s\r\n", fieldname.c_str());
			filed.Dump();

		}
		template <typename T, typename Enable = EnableIfHasMemberList<T>>
		static void DumpWrapper(nop::S7PlusPacketValue<T> filed, std::string fieldname)
		{
			printf("fieldname:=>%s\r\n", fieldname.c_str());
			filed->Dump();

		}

文章来源:https://blog.csdn.net/oShuangYue12/article/details/134893429
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。