【发布时间】:2017-11-06 06:04:47
【问题描述】:
我正在尝试创建一个具有基于另一个模板的模板返回类型的函数,我也想将其默认为默认值。很难解释,但这里是代码:
template<template<typename ReturnType = float, typename> DurationType = std::chrono::duration<ReturnType, std::milli> >
ReturnType tick()
{
high_resolution_clock::time_point currentPoint = high_resolution_clock::now();
DurationType elapsed = currentPoint - mStartTimePoint;
mStartTimePoint = currentPoint;
return elapsed.count();
}
显然上面没有编译,但我想做的是让函数返回ReturnType(float 或double 可能)并指定返回类型的单位。在这种情况下,我希望它默认为毫秒。这可能吗?
【问题讨论】:
-
你想在这里实现什么还不清楚。为什么将
DurationType提升为函数的模板参数,而它甚至没有出现在它的界面中?您可以将ReturnType作为您唯一的模板参数,并将DurationType替换为auto。
标签: c++ c++11 templates chrono