0

I was looking at some code block in C++ and come across these two statements -

using xx = double;
using yy = std::complex< val >

Can you please help me to understand above 2 statement and why there are required to be defined in this way?

Daniel
  • 121
  • 6
  • 2
    If the code you're looking at changes perfectly readable types like `double` into `xx` then go and look at some better code. For documentation on `using` see https://en.cppreference.com/w/cpp/language/type_alias – Alan Birtles Sep 03 '20 at 14:34
  • Given the previous line `std::complex< val >` *might not* be [unspecified behaviour](https://en.cppreference.com/w/cpp/numeric/complex), but I don't hold out hope – Caleth Sep 03 '20 at 14:36
  • It's just a type alias. After this statement, both `yy` and `std::complex` are the same type name and can be used interchangeably. `yy` is not a useful name, but `CVal` or `ValComplex` for example could be. – Yksisarvinen Sep 03 '20 at 14:59
  • @Yksisarvinen - why it is required to define a new type in this way that appears to be adding no extra value? Developer just could work with Double, isn't it? – Daniel Sep 04 '20 at 16:20
  • @Daniel Certainly, and you likely should use `double` instead of thinking of a new name for that. But imagine I have to use this monstrosity: `std::map>>` I really, really don't want to type this every time I use this container, because it would blotch the readability of my code, so I can typedef it (with using statement) to something nicer like `MyClassContainer`. – Yksisarvinen Sep 04 '20 at 19:49
  • @Daniel And then there are some things that are hard to read just because C++ syntax makes them so, like function pointers. If I need to pass function pointer to `foo`, I can write `void foo (char*(*funcPtr)(char**, int))`, but I can also typedef it first: `using FuncPtr = char*(*)(char**, int)` and then declare `foo` as `void foo (FuncPtr funcptr)` – Yksisarvinen Sep 04 '20 at 19:54
  • Thanks. How exactly could we make std::map>> more concise using the typedef? Apologies if my question is very trivial I am just teaching myself the C++ practises – Daniel Sep 05 '20 at 09:23

0 Answers0