Function Template: Solve Complications with Mixing Types for Variable Swapping

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    Function
Click For Summary
The discussion revolves around the challenges of using templates in C++ to swap variables of different types, specifically when using references. The original attempt to swap an integer and a double using a template function resulted in compilation errors due to type incompatibility with references. A proposed solution involves creating a template function that accepts two different types as parameters, allowing for the swapping of mixed types. The conversation also touches on the importance of understanding how templates work and the need for explicit type casting when dealing with different data types. Ultimately, the participants emphasize the significance of learning and applying templates correctly in programming.
  • #61
Don't tell me in this case, cheapness doesn't pay. I downloaded Gaddis 6th edition for free, it has nothing of this kind at all. Wow, maybe I should pay some money and get a newer version. Now I have to guess reading codes!

Not just loop range based or the if else statement explained before. Don't they have a general name for this kind of simplified code. I just want to find a website that show the translation. I don't even know the name. I hate to ask every time I encounter this kind of simplified code. I am sure I can learn it just like that if I can have a table of translation.

Thanks
 
Technology news on Phys.org
  • #62
yungman said:
Don't tell me in this case, cheapness doesn't pay. I downloaded Gaddis 6th edition for free, it has nothing of this kind at all.
Gaddis's 6th ed. is copyrighted 2009, before the changes in C++. As John Arbuckle once said, "You get what you pay for." (Catch phrase used in ads for Yuban coffee)
yungman said:
Don't they have a general name for this kind of simplified code.
Not sure which simplified code you're asking about. Do you mean the braced initializer list (e.g.,
T result{ data[0] }; ) ?
 
  • Like
Likes Vanadium 50
  • #63
yungman said:
Don't they have a general name for this kind of simplified code.
New features? C++ is a constantly evolving language, any book will become out of date.

yungman said:
I just want to find a website that show the translation.
I mostly use cppreference.com. As you can see there are new features introduced in c++20 (for example the spaceship operator), and there will be more coming in c++23.
 
  • Like
Likes yungman
  • #64
Hi

I have been working on new stuffs using the book by Ivor, things that I never learned because the Gaddis I use is too old. So I have to take a detour to learn those. I think I cover the function templates quite well at this point. I just want to verify with you guys on my notes I have on typeid(), auto, decltype() and Trailing return type.

I find I like GeekForGeeks the best. It's the easiest to understand.

I attached my notes, I hope I am correct, if not, please let me know.

Thanks
 

Attachments

Similar threads

  • · Replies 19 ·
Replies
19
Views
5K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 0 ·
Replies
0
Views
484
  • · Replies 39 ·
2
Replies
39
Views
4K
  • · Replies 89 ·
3
Replies
89
Views
6K
Replies
20
Views
2K
Replies
4
Views
5K
  • · Replies 0 ·
Replies
0
Views
973
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 15 ·
Replies
15
Views
3K