您可以使用typedef为类型创建更短更简单的名称:
typedef std::chrono::high_resolution_clock Clock;
typedef Clock::time_point TimePoint;
typedef std::chrono::seconds Seconds;
typedef std::chrono::milliseconds Milliseconds;
Run Code Online (Sandbox Code Playgroud)
以及实例化的模板化类型:
typedef std::chrono::duration<float, std::ratio<1>> RealDuration;
// Example usage
float dt = RealDuration(a - b).count();
Run Code Online (Sandbox Code Playgroud)
对于函数指针:
typedef void (*FuncPtr)(int,int);
Run Code Online (Sandbox Code Playgroud)
您还可以为模板使用类型别名:
template<typename T> using uptr = std::unique_ptr<T>;
// Example usage
uptr<int> myInt;
uptr<foo> myFoo;
Run Code Online (Sandbox Code Playgroud)
但是如何创建一个模板函数的别名/指针?例如,如果我希望能够使用名称DurationCast来编写如下内容:
x = DurationCast<Seconds>(a - b);
y = DurationCast<Milliseconds>(c - d);
Run Code Online (Sandbox Code Playgroud)
需要做些什么来缩短函数std::chrono::duration_cast<T>(),DurationCast<T>()而不是简单地去using namespace std::chrono;或using std::chrono::duration_cast;路由,而不用编写我自己的函数对象来实现它?
编辑: 我想我可以写一个简单的包装器:
template<typename ToType, typename FromType>
ToType DurationCast(const FromType& d)
{
return …Run Code Online (Sandbox Code Playgroud) std::condition_variable::wait_for采用可选谓词在内部处理虚假唤醒. std::future::wait_for没有任何这样的可选参数.如果我想确保至少等待指定的超时时间,或者是否已经以其他方式处理,那么我需要防范虚假唤醒吗?