Vincent
Vincent

Reputation: 60381

Return type of bitwise operators in C++

As I'm implenting templated classes for small math vectors, I encounter one problem. For the arithmetic operations, the return type of T1 lhs + T2 rhs is std::common_type<T1, T2>::type. But what is the return type for the following (for example T1 signed and T2 unsigned or the contrary, or T1 char and T2 unsigned long long int etc...) :

T1 lhs & T2 rhs ?
T1 lhs | T2 rhs ?
T1 lhs ^ T2 rhs ?
T1 lhs << T2 rhs ?
T1 lhs >> T2 rhs ?

Thank you very much.

Upvotes: 6

Views: 2395

Answers (1)

Sergey K.
Sergey K.

Reputation: 25386

I assume you are going to implement a compoment-wise bitwise operations on vectors. Essentially bitwise operations are integer operations and i see no reason why not to make their result as std::common_type<T1, T2>::type.

The result of shifts does not depend on the right operand. Just use T1 for it.

Upvotes: 4

Related Questions