C++ Weekly - Ep 302 - It's Not Complicated, It's std::complex

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to c plus plus weekly i'm your host jason turner available for code reviews and on-site training in this episode i want to cover a little corner of the standard library that i realized people weren't familiar with and that's standard complex [Music] now this one is almost certainly going to be a short one and i know i say that from time to time and ends up being longer than i expected but here's standard complex it's in the well perhaps not surprising complex header there you go now this is complex numbers that's all that it is you've gotten a real and an imaginary part and you can specify if you want float double long double whatever these specializations exist representing and manipulating complex numbers it says can we make a complex of int i don't know we can give that a try in a moment but complex has been around since the beginning of the c plus standard library it's one of the original types that was supported in c plus 98 it has continued to evolve very slowly but it is not a completely forgotten corner of the standard library so if we look at our definition here we can actually see that operator equals an operator not equals were actually removed in c plus plus 20. and if you've been paying attention then you might have an idea as to why these were removed in c plus plus 20. the not equals operator is now synthesized from the equals equals operator in c plus 20. so they removed explicit definitions of things where they could and took advantage of the fact that we've got this ability to explicitly default operators comparison operators and c plus plus 20 due to the spaceship operator additions in c plus 20 which you can check out my other episode on the spaceship operator if you haven't already seen that one so we can see that these things were removed and some things were changed and added in c plus plus 14. for example here we can see that we got const expert support for comparison operators and c plus plus 14 for equals and not equals and in c plus plus 14 we got user defined literals so you can actually do things like this to express that you want a 1 plus two imaginary a complex number expressed as one might expect to see an imaginary number expressed in a math setting in a c plus plus 14 and we got ponstex for support for getting the real and imaginary components in c plus plus 14. and interestingly as i've pointed out many times before on the show some things didn't get const expert support nearly as early as they could have but we got constexpr mathematical operator support in c plus plus 20. so as you can see things have continued to grow and develop however you might notice that things like sine and power and cosine aren't going to have constants for support and that is probably because they rely on the c standard library functions for these things note c math here and c math does not yet have constructs for support for things like sine and cosine so hopefully in c plus plus 23 we'll get that and then complex will have another round of updates to do to it for more context for support it does seem though that since we have this up here we have to kind of play with this complex support let's look at our constructors and these constructors were made const expert and c plus plus 14. although interestingly the copy constructor and this constructor float and double our c plus 11 but for the base template the non-specialized template these things weren't made const expert until c plus plus 14. perhaps there was some reason for it i have no idea [Music] so we can get access to the real or imaginary parts of the complex number with no problem here and that's a double and just for the fun of it let's see if we can make an integer based complex number there we go so i don't tend to do a lot of complicated math computational kinds of things in my c plus work but i have used standard complex several times in various implementations of my mandelbrot renderer which i covered in the parallel algorithms episode so you can see other use of standard complex there so if you have any use for complex numbers in c plus plus make sure you check this out it is updated it should be efficient it does have constructs for support and as always thank you for watching this episode of c plus weekly be sure to subscribe give this video a thumbs up [Music] [Music] you
Info
Channel: Cᐩᐩ Weekly With Jason Turner
Views: 2,549
Rating: undefined out of 5
Keywords: C++, Programming, c++ tutorial, c++ basics, learning c++, c++ programming, c++ weekly
Id: s_1SymtU0BI
Channel Id: undefined
Length: 6min 13sec (373 seconds)
Published: Mon Dec 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.