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

Discussion Overview

The discussion revolves around the challenges of implementing a function template in C++ that allows for swapping variables of different types, specifically mixing `int` and `double`. Participants explore various approaches to achieve this functionality while addressing issues related to passing by reference and type compatibility.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes an attempt to swap an `int` and a `double` using a template function but encounters errors when using references for arguments.
  • Another participant suggests a potential solution involving two template arguments to allow for explicit casting during the swap process.
  • Some participants emphasize that `int` and `double` are fundamentally different types, which complicates the swapping process when using a single template type.
  • A participant points out that template functions are typically used for operations on the same type, suggesting that the original approach may not align with standard practices.
  • Examples of other template functions for addition and multiplication are shared, illustrating how templates can be effectively used with the same type.
  • One participant expresses uncertainty about the complexity of the original template function from a referenced book, indicating a preference for simpler examples.
  • Another participant notes the importance of understanding the definitions of template functions before commenting on their implementation.

Areas of Agreement / Disagreement

Participants generally do not reach a consensus on the best approach to swap variables of different types, with multiple competing views and suggestions remaining throughout the discussion.

Contextual Notes

Some limitations are noted regarding the assumptions made about type compatibility and the specific implementations of template functions, which are not fully detailed in the discussion.

  • #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   Reactions: 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   Reactions: 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
6K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 89 ·
3
Replies
89
Views
6K
Replies
20
Views
2K
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 15 ·
Replies
15
Views
3K