C++并发编程实战(C++ Concurrency in Action)书中代码问题

小可C 2017-01-07 11:49:13
4.3.3 时间点:P95页
auto start=std::chrono::high_resolution_clock::now();
do_something();
auto stop=std::chrono::high_resolution_clock::now();
std::cout<<”do_something() took “
<<std::chrono::duration<double,std::chrono::seconds>(stop-start).count() //这里出错,出现十几个错误,使用vs2015
<<” seconds”<<std::endl;
以上是书中代码,错误原因是什么?

改成std::chrono::duration<double,std::ratio<1,1>>(stop-start).count() 就没错,也不知道改得对不对,刚学的
...全文
277 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ID870177103 2017-01-07
  • 打赏
  • 举报
回复
[code=c]template<class _Clock,
	class _Duration1,
	class _Duration2> inline
	constexpr typename common_type<_Duration1, _Duration2>::type
		operator-(
			const time_point<_Clock, _Duration1>& _Left,
			const time_point<_Clock, _Duration2>& _Right)
	{	// add time_point to time_point
	return (_Left.time_since_epoch() - _Right.time_since_epoch());
	}

	template<class _Rep2,
		class _Period2,
		class = typename enable_if<treat_as_floating_point<_Rep>::value

			|| (_Ratio_divide_sfinae<_Period2, _Period>::den == 1

				&& !treat_as_floating_point<_Rep2>::value),
			void>::type>
		constexpr duration(const duration<_Rep2, _Period2>& _Dur)
			: _MyRep(chrono::duration_cast<_Myt>(_Dur).count())
		{	// construct from a duration
		}
如上代码,可知duration函数的参数取决于 std::chrono::high_resolution_clock::now ()的返回类型 这个类型在不同机器上也许有所不同 正确的做法是让编译器自己去推导duration的参数类型 std::chrono::duration<double> (stop - start).count () ;[/code]

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧