How can I identify if the number is complex in C++?
Is there any built in function like this?:
isComplex(1)->false
How can I identify if the number is complex in C++?
Is there any built in function like this?:
isComplex(1)->false
Copyright © 2021 Jogjafile Inc.
C++ is a strongly typed language, and the literal
1is always anint.The determination you ask about might be relevant when converting text...
isComplex("1"), for that you can attempt streaming:Separately, if you're inside a template and not sure whether a type parameter is
std::complex, you can test with e.g.std::is_same<T, std::is_complex<double>>::value, for example:Output:
See the code here.
For more complicated functions, you may want to create separate overloads for e.g.
doubleandstd::complex<double>, and/orfloatandstd::complex<float>,long doubleetc..