2

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?

  • 1
    Yes, it is, but you should probably use `signed char` or `unsigned char` instead. *Unlike* `short`, `int`, etc., `signed char` is a distinct type from `char`, so you probably don't want to do arithmetic with it (you don't know if it's signed in general). – user541686 Aug 18 '12 at 11:46
  • 2
    If you're a perfectly moral being, you should only consider `signed char` and `unsigned char` as arithmetic types, and reserve `char` purely as a system-interfacing I/O type. – Kerrek SB Aug 18 '12 at 11:55
  • @Mehrdad Thank you. I will never use char for arithmetic operations! ;). In fact I thought char was _italic_not_italic_ an arithmetic type... –  Aug 18 '12 at 12:14

2 Answers2

5

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

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';