[英]Wrapping boost::chrono timing mechanism into class compilation errors?
我遇到的問題與在類似的類型t1_t0 = t1 - t0;
相似類型之間聲明操作的結果類型有關t1_t0 = t1 - t0;
在a.cpp中 t1
和t0
分別是最后時間和初始時間,差值用於測量一次計算之間的時間跨度。 我嘗試在整個函數中使用auto
代替變量boost::chrono::high_resolution_clock::time_point
, std::chrono::seconds
boost::chrono::high_resolution_clock::time_point
std::chrono::seconds
和int
但是在調用函數之前,在聲明變量時引起了奇怪的問題它。
它被稱為error: use of before deduction of 'auto'
編譯錯誤error: use of before deduction of 'auto'
因此我嘗試使用實際類型來編譯項目,現在遇到了麻煩。 如果在int main
之上聲明了該類,則該項目的編譯效果很好(使用auto
), 但是如果將該類分為頭文件和cpp文件,則該項目將無法正常工作 。
這是整個項目,請幫忙!
#include <iostream>
#include "a.h"
int main()
{
a A;
int timer = A.time();
std::cout<<timer<<std::endl;
return 0;
}
這是啊
#ifndef A_H
#define A_H
#include <iostream>
#include <boost/chrono/chrono_io.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
#include <sstream>
#include <cassert>
#include <chrono>
class a
{
private:
public:
a();
~a();
int time();
};
#endif
這是a.cpp
#include "a.h"
a::a(){}
a::~a(){}
int a::time()//simple benchmark timer
{
boost::chrono::high_resolution_clock::time_point t0, t1, t1_t0;
t0 = boost::chrono::high_resolution_clock::now();
//Run a process to measure time.
t1 = boost::chrono::high_resolution_clock::now();
t1_t0 = t1 - t0;//Not sure what the resulting type is here?
std::chrono::seconds nsec = std::chrono::duration_cast<std::chrono::seconds>(t1_t0);
//Not sure what the resulting type is here?
return nsec.count();//Not sure what the return type is here?
}
這是編譯它的錯誤。
||=== Build: Debug in return_type_auto_from_class (compiler: GNU GCC Compiler) ===|
/home/Desktop/a.cpp||In member function ‘int a::time()’:|
/home/Desktop/a.cpp|14|error: no match for ‘operator=’ (operand types are ‘boost::chrono::steady_clock::time_point {aka boost::chrono::time_point<boost::chrono::steady_clock>}’ and ‘boost::common_type<boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >, boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> > >::type {aka boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >}’)|
/usr/include/boost/chrono/time_point.hpp|156|note: candidate: constexpr boost::chrono::time_point<boost::chrono::steady_clock>& boost::chrono::time_point<boost::chrono::steady_clock>::operator=(const boost::chrono::time_point<boost::chrono::steady_clock>&)|
/usr/include/boost/chrono/time_point.hpp|156|note: no known conversion for argument 1 from ‘boost::common_type<boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >, boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> > >::type {aka boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >}’ to ‘const boost::chrono::time_point<boost::chrono::steady_clock>&’|
/usr/include/boost/chrono/time_point.hpp|156|note: candidate: constexpr boost::chrono::time_point<boost::chrono::steady_clock>& boost::chrono::time_point<boost::chrono::steady_clock>::operator=(boost::chrono::time_point<boost::chrono::steady_clock>&&)|
/usr/include/boost/chrono/time_point.hpp|156|note: no known conversion for argument 1 from ‘boost::common_type<boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >, boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> > >::type {aka boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >}’ to ‘boost::chrono::time_point<boost::chrono::steady_clock>&&’|
/home/Desktop/a.cpp|16|error: no matching function for call to ‘duration_cast(boost::chrono::steady_clock::time_point&)’|
/usr/include/c++/5/chrono|194|note: candidate: template<class _ToDur, class _Rep, class _Period> constexpr typename std::enable_if<std::chrono::__is_duration<_Tp>::value, _ToDur>::type std::chrono::duration_cast(const std::chrono::duration<_Rep, _Period>&)|
/usr/include/c++/5/chrono|194|note: template argument deduction/substitution failed:|
/home/Desktop/a.cpp|16|note: ‘boost::chrono::steady_clock::time_point {aka boost::chrono::time_point<boost::chrono::steady_clock>}’ is not derived from ‘const std::chrono::duration<_Rep, _Period>’|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|
編輯后的代碼,它可以修復原始代碼,並且現在可以使用。
int a::time()
{
boost::chrono::high_resolution_clock::time_point t0, t1;//, t1_t0;
t0 = boost::chrono::high_resolution_clock::now();
//Run a process to measure time.
boost::this_thread::sleep_for( boost::chrono::milliseconds{ 2000});
t1 = boost::chrono::high_resolution_clock::now();
auto t1_t0 = t1 - t0;
auto nsec = boost::chrono::duration_cast<boost::chrono::seconds>(t1_t0);
return nsec.count();
}
不要混合boost::chrono
和std::chrono
。 使用一個或另一個。 我推薦std::chrono
如果您的平台提供並支持它),否則請使用boost::chrono
。
兩個time_point
之間的差是duration
,而不是另一個time_point
。 例如,“明天”和“今天”可以被視為時間點(具有非常粗略的精度)。 tomorrow - today == 1 day
。 一天是持續時間。
如果您希望最終結果以納秒為單位,則無需如此努力:
nanoseconds nsec = t1 - t0;
如何從a::time()
返回nanoseconds
而不是int
呢? 這樣,客戶端就具有類型安全的持續時間,而不是可能意味着任何含義的int
。
return t1 - t0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.