The return type of bitwise operators in C ++

Since I am introducing template templates for small mathematical vectors, I am faced with one problem. For arithmetic operations, the return type 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 vice versa, 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 ? 

Many thanks.

+6
source share
1 answer

I assume that you are going to implement bitwise operations on vectors by command. In essence, bitwise operations are operations with integers, and I see no reason why not make their result like std::common_type<T1, T2>::type .

The result of the shifts does not depend on the correct operand. Just use T1 for this.

+4
source

Source: https://habr.com/ru/post/921788/


All Articles