下面的代码片段是一个游戏,编译器抱怨返回值,所以我想要一些关于如何做这个技巧的反馈,让一个函数根据输入的类型返回两种不同的类型,但不重载
template <typename T>
T GetTimeDead(uint64 Guid)
{
bool stringOutput;
if(typeid(T) == typeid(float))
stringOutput = false;
else
stringOutput = true;
bool found = false;
for(map<uint32, TrackInfo>::iterator itr = dieTracker.begin(); itr != dieTracker.end(); ++itr)
{
if(itr->second.GUID == Guid)
{
found = true;
break;
}
}
if(!found)
stringOutput ? return "never" : return sObjectMgr->FindCreature(Guid)->GetCreatureData()->spawntimesecs;
if(!stringOutput)
return dieTracker.find(Guid)->second.seconds;
float seconds = dieTracker.find(Guid)->second.seconds;
uint64 secs = seconds % 60;
uint64 minutes = seconds % 3600 / 60;
uint64 hours = seconds % 86400 / 3600;
uint64 days = seconds / 86400;
ostringstream ss;
if(days)
days != 1 ? ss << days << " Days " : ss << days << " Day ";
if(hours)
hours != 1 ? ss << hours << " Hours" : ss << hours << " Hour";
if(minutes)
minutes != 1 ? ss << minutes << " Minutes " : ss << minutes << " Minutes ";
if(secs || (!days && !hours && !minutes))
secs != 1 ? ss << secs << " Seconds " : ss << secs << " Second ";
ss << "ago";
return ss.str();
}