jeudi 1 juin 2017

Wrapping boost::chrono timing mechanism into class compilation errors?

The problem I have pertains to the declaration of the resulting type of an operation between to similar types written as t1_t0 = t1 - t0; in a.cpp. t1 and t0 are the final and initial times respectively with the difference being used to measure the time span between one calculation. I tried using auto all over the function in place of the variables boost::chrono::high_resolution_clock::time_point, std::chrono::seconds and int but that caused strange problems with declaring variables after the fact that the function was called before it.

It was called the error: use of before deduction of ‘auto’ compilation error so I then tried to compile the project using the real types and now I have this trouble. The project does compile fine (using auto) if the class is declared above int main but it doesn't work if the class is separated into header and cpp files.

Here's the whole project, please help!

#include <iostream>
#include "a.h"

int main()
{
    a A;

    int timer = A.time();
    std::cout<<timer<<std::endl;

    return 0;
}

this is a.h

#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

this is the 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?
}

Here are the errors compiling it.

||=== 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)) ===|

Aucun commentaire:

Enregistrer un commentaire