Why acts std::chrono::duration::operator*= not like built-in *=?

Why acts std::chrono::duration::operator*= not like built-in *=?

As described in&nbsp;<a href="https://en.cppreference.com/w/cpp/chrono/duration/operator_arith3" target="_blank">std::chrono::duration::operator+=</a>&nbsp;the signature is

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 built-in, 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 &lt;&lt; " 150% of 10: " &lt;&lt; i &lt;&lt; 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.

EDIT: Thanks for your responses, I know that the implementation behaves that way and how I could handle it. My question is, why is it designed that way.

I would expect the conversion to int take place at the end of the operation. In the following example both operands get promoted to double before the multiplications happens. The intermediate result of 4.5 is converted to int afterwards, so that the result is 4.

int i = 3;
i *= 1.5;
assert(i == 4);

My expectation for std::duration would be that it behaves the same way.

c++

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Dicey Issues in C/C++

C/C++ problems. If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends.

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

A Complete Guide to fread() in C/C++

In this article, we’ll take a look at using fread() in C/C++.