Char is an arithmetic type?
-
27-06-2021 - |
سؤال
I created a Matrix class, using
static_assert(std::is_arithmetic<T>::value,"");
to check if the template type is an arithmetic type. So I tried with
Matrix<char> matrix1(3,3); // ctor takes number of rows and columns
and it works. The static_assert function is not called with char type. It is normal? char is seen like an arithmetic type?
المحلول
From the reference:
If T is an arithmetic type (that is, an integral type or a floating-point type), provides the member constant value equal true. For any other type, value is false.
char
is an integral type, so the answer is true
. The fact that the small integers that fit in a char
are often interpreted as codepoints in a particular character encoding space is secondary.
نصائح أخرى
Yes, it's just as a normal integer as any other (int, long, short). That's also a common practice in C(++) to use it for arithmetic, e. g. for converting a digit to its corresponding printable character, you can write
char printable = digit + '0';