跳到主内容
C++
文章阅读

c++使用type_identity_t实现泛型自动推导

2025/12/1697 次阅读2 分钟

/**
 * 省略写if语句
 * @tparam ObjectType 指针类型
 * @param Objc 传入的指针
 * @param Call 执行有效执行
 */
template <typename ObjectType>
requires std::is_pointer_v<ObjectType>
static void Let(ObjectType Objc, std::function<void(std::type_identity_t<ObjectType>)> Call)
{
	if (Objc)
	{
		Call(Objc);
	}
}


// 用法
Let(PathComponent, [this](auto Path)
	{
		if (SplinePointCount > 0)
		{
			if (CurrentSplinePointIndex < SplinePointCount)
			{
				const auto Local = Path->GetSplineLocationByIndex(CurrentSplinePointIndex);
				MyPrintfDebug("开始往样条点移动....");
				UE_LOG(LogTemp,Warning,TEXT("开始移动到样条点,样条点数量:%d"),Path->GetPointCount());
				auto Result = MoveToLocation(Local); //往样条点位置移动
				if (Result == EPathFollowingRequestResult::RequestSuccessful)
				{
					MyPrintfDebug("成功了..");
				}else
				{
					MyPrintfDebug("失败了");
				}
				CurrentSplinePointIndex++;
			}
			else
			{
				///停止移动
				MyPrintfDebug("移动完成了");
			}
		}
	});

is_pointer_v: 限制泛型为指针类型,除了这个,还有很多限制 image.png

返回顶部