vendredi 22 février 2019

Why acts std::chrono::duration::operator*= not like POD *=?

As described in std::chrono::duration::operator+= the signature is

duration& operator*=(const rep& rhs);

This makes me wonder. I would assume that a duration literal can be used like any other POD, but it doesn't.

#include <chrono>
#include <iostream>

int main()
{
    using namespace std::chrono_literals;
    auto m = 10min;
    m *= 1.5f;
    std::cout << " 150% of 10min: " << m.count() << "min" << std::endl;

    int i = 10;
    i *= 1.5f;
    std::cout << " 150% of 10: " << i << std::endl;
}

Output is

150% of 10min: 10min
150% of 10: 15

Why was the interface choosen that way? To my mind, an interface like

template<typename T> 
duration& operator*=(const T& rhs);

would yield more intuitive results.

Aucun commentaire:

Enregistrer un commentaire