Comparing chrono::duration with an integer

Rrz*_*rz0 1 c++ c++-chrono

I am trying to compare the time elapsed to execute a piece of code with a fixed integer.

For example:

auto startProcessingTime = chrono::high_resolution_clock::now();
chrono::duration<int> timeDiff = (chrono::duration_cast<chrono::seconds>(chrono::high_resolution_clock::now() - startProcessingTime));

    if (timeDiff > 12) {
        // DO SOMETHING
        continue;
    }
Run Code Online (Sandbox Code Playgroud)

However on running this I get the following error:

Invalid operands to binary expression ('chrono::duration' and 'int')

How can I convert timeDiff to an integer?

I have also tried:

chrono::seconds s = chrono::duration_cast<chrono::seconds>(timeDiff);
Run Code Online (Sandbox Code Playgroud)

However,

Invalid operands to binary expression ('chrono::seconds' (aka 'duration') and 'int')

Any help would be greatly appreciated.

Nik*_* C. 7

您需要告诉它什么12意思。是几秒钟?毫秒?因此,要么强制转换:

chrono::seconds(12)
Run Code Online (Sandbox Code Playgroud)

或者(我最喜欢的)将其设为chrono文字。如果您的意思是12秒,则:

using namespace std::chrono_literals;
// ...

if (timeDiff > 12s) {
Run Code Online (Sandbox Code Playgroud)

如果是毫秒:

if (timeDiff > 12ms) {
Run Code Online (Sandbox Code Playgroud)