mercredi 6 novembre 2019

How use std::chrono::milliseconds as a default parameter

I have a C++ function which intakes a parameter as std::chrono::milliseconds. It is basically a timeout value. And, it is a default parameter set to some value by default.

#include <iostream>
#include <chrono>

void Fun(const std::chrono::milliseconds someTimeout = std::chrono::milliseconds(100)) {
    if (someTimeout > 0) {
        std::cout << "someNumberInMillis is: " << someNumberInMillis.count() << std::endl;
    }
}

int main() {
    unsigned int someValue = 500;
    Fun(std::chrono::milliseconds(someValue))
}

All of above is okay but, when I call Fun with a value then fails to compile and I get teh following error:

No viable conversion from 'bool' to 'std::chrono::milliseconds' (aka 'duration >')

What am I doing wrong here? I want the caller of Fun to eb explicitly aware that it is passing chrono::milliseconds when it invokes Fun. But the compiler doesn't seem to allow using std::chrono::milliseconds as a parameter?

How use std::chrono::milliseconds as a default parameter

Aucun commentaire:

Enregistrer un commentaire