static_assert compilation error with is_arithmetic<> type predicate

Hi

The following use of static_assert with the is_arithmetic<> type predicate generates a compilation error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <type_traits>

template<typename Scalar>
class Complex
{
   Scalar re, im;

public:
   Complex(Scalar re, Scalar im)
   {
      static_assert(is_arithmetic<Scalar>(),
                    "complex is supported only for arithmetic types");

      this->re = re;
      this->im = im;
   }
};


int main()
{
   Complex<int> ci(5, 6);
   Complex<string> cs("A", "B");
}


Compilation error is:

|In instantiation of 'Complex<Scalar>::Complex(Scalar, Scalar) [with Scalar = 
std::basic_string<char>]':|
|error: static assertion failed: complex is supported only for arithmetic types|


Actually, there is no instantiation of Complex<Scalar> with <char> at all. What's the problem here?

Thanks
Last edited on
std::string is a typedef for std::basic_string<char>.
Thanks for the clarification.

This means that in fact the program worked correctly!
Topic archived. No new replies allowed.