Function Template: Solve Complications with Mixing Types for Variable Swapping

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    Function
AI Thread 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.
  • #51
The third example would be ridiculous of course, I only wrote it to demonstrate that in c++ the amount of whitespace (i.e. spaces, tabs and new lines) separating symbols doesn't matter - pretty much anywhere we can put a space, we can start a new line, and anywhere we have a new line we can replace it with a space.

The important exceptions to this are compiler directives, which are not actually part of c++ itself, and these always start with # as the first character of a new line, and in // style comments which are terminated by the first following newline.

So to summarise, it doesn't matter how you lay out the template function declaration, it must always consist of exactly 3 parts separated by whitespace:
C++:
template <class identifier> function_declaration;
// or
template <typename identifier> function_declaration;

We can of course have as many template function definitions as we like: each one must start with the template keyword, followed by a class or typename identifier, followed by a function declaration.
 
Last edited:
Technology news on Phys.org
  • #52
Hi Pbuk

Thanks for all the info, I have been out and busy the whole day, this is the first change I even get to print it out and read it. I'll read your posts and I'll be back tomorrow.

Thanks so much. Sorry I did not come back sooner.
 
  • #53
Thank you so much Pbuk. I got it. You verify that my guess in post 35 is correct. Finally it makes sense that template<class T> has to go together with the function. This is what I missed before. I actually made this program with the modification and it works:
C++:
#include <iostream>
using namespace std;
template<class T>  void swapVars(T &var1, T &var2)
{    T temp;
    temp = var1;
    var1 = var2;
    var2 = temp;
}
template<class T> T larger(T var3, T var4)
{
    if (var3 > var4) return var3;
    else if (var4 > var3)return var4; 
    return var3;
}

int main()
{
    int a=4,b=1;//test T as int
    cout<<" larger of big and small is: "<<larger(a, b)<<endl;

    double c = 2.1, d = 3.5;//testing T is double
    swapVars(c, d);
    cout << " After swapping " << c << ", " << d << endl;
    return 0;
}

I like to write template<class T> void swapVars(T &var1, T &var2) in one line to remind me they have to go together. I even made the two function templates so one work on int and the other work on double to proof I can use template<class T> and don't have to make it template<class T1, class T2> to accommodate two functions.

Finally, you wrote return (a>b?a:b). What do you call this form: (a>b?a:b)? I would have to write in long form:
C++:
if(a>b) return a;
else return b;

I want to learn how to write in this form, I don't even know what you call that so I can't even search online.

Thanks so much.
 
  • #54
  • Like
Likes yungman
  • #55
yungman said:
I like to write template<class T> void swapVars(T &var1, T &var2) in one line to remind me they have to go together.
In this case I think I agree with you - it is clearer and it doesn't make the line too long.
 
  • Like
Likes yungman
  • #56
Thanks so my Pbuk, now I have no issue working through template so far.

But I have questions on the "short" form of writing statements like the a>b ? a:b type. seems like there is a new way of writing program from what I learn in the old Gaddis book. I don't even know what you call these. Last time when I asked you, that turn out to be a very specific if else statement, but there are a lot more like the example below. What do you call those simplified statement. I need to learn this.

Below are a few examples I have no idea where they come from. I put the questions in the comments:
C++:
#include<iostream>
#include<vector>
using namespace std;
template<typename T1, typename T2>
? ? ? larger(T1 a, T2 b)// what is ?
{
    return a > b ? a : b;
}

template<typename T1, typename T2>
auto larger(T1 a, T2 b)// what is auto
{
    return a > b ? a : b;
}

template<type T> T larger(const vector<T>& data)
{
    T result{ data[0] }; //why not T result = data[0]?
    for(auto& value : data)//what is this?
    {
        if (value > result)result = value;
    }
    return result;
}

Note that it's not the template that I have question, it's the short form statement that I don't get.

I want to get the name of this short form so I can look up and learn. Without the name, I don't even know what to look.

Thanks so much for helping.
 
  • #57
The <condition> ? <expression> : <expression> form is called a ternery condition, or look up ternery operator. Auto means it doesn't check the type that is returned from the function (note that if a > b then a will be returned which has type T1 otherwise b will be returned which has type T2).

That's two for you to get started on.
 
  • #58
yungman said:
But I have questions on the "short" form of writing statements like the a>b ? a:b type.
As pbuk already noted, this is the conditional operator, also called the ternary operator because it has three operands. This operator has been present in C from the beginning, and was inherited by C++. It's also present in Java, C#, Python, Perl, and probably a few others.

The expression a > b ? exp1 : exp2 consists of three parts:
  1. The first part (a > b above) is a Boolean expression, one that evaluates to true or false.
  2. The second part (exp1).
  3. The third part (exp2).
If the Boolean expression is true, the conditional expression evaluates to exp1.
If the Boolean expression is false, the conditional expression evaluates to exp2.

C++:
int pets = 2;
std::cout << "I have " << pets << " pet" << (pets > 1 ? 's' : '\0') << std::endl;

If pets is 1, the conditional expression evaluates to the null character, and the output will be "I have 1 pet". If pets is 2, conditional expression evaluates to the character literal 's', and the output will be "I have 2 pets", and similar for other integers larger than 1. Additional logic would be needed if pets is 0 or negative.

The Gaddis book, "Starting out with C++, from Control Structures through Objects," 6th Ed, shows the conditional operator on the 2nd page of the book, and in section 4.14, starting on page 218.
 
  • Like
Likes Vanadium 50
  • #59
Mark44 said:
As pbuk already noted, this is the conditional operator, also called the ternary operator because it has three operands. This operator has been present in C from the beginning, and was inherited by C++. It's also present in Java, C#, Python, Perl, and probably a few others.

The expression a > b ? exp1 : exp2 consists of three parts:
  1. The first part (a > b above) is a Boolean expression, one that evaluates to true or false.
  2. The second part (exp1).
  3. The third part (exp2).
If the Boolean expression is true, the conditional expression evaluates to exp1.
If the Boolean expression is false, the conditional expression evaluates to exp2.

C++:
int pets = 2;
std::cout << "I have " << pets << " pet" << (pets > 1 ? 's' : '\0') << std::endl;

If pets is 1, the conditional expression evaluates to the null character, and the output will be "I have 1 pet". If pets is 2, conditional expression evaluates to the character literal 's', and the output will be "I have 2 pets", and similar for other integers larger than 1. Additional logic would be needed if pets is 0 or negative.

The Gaddis book, "Starting out with C++, from Control Structures through Objects," 6th Ed, shows the conditional operator on the 2nd page of the book, and in section 4.14, starting on page 218.
Yes, I understand that already, it's the other news ones in my post.

Thank
 
  • #60
yungman said:
Yes, I understand that already, it's the other news ones in my post.
Maybe it's some of these:
C++:
template<typename T1, typename T2>
? ? ? larger(T1 a, T2 b)// what is ?
{
    return a > b ? a : b;
}
I don't think this is meant to be actual code. As far as I can tell, the ? is just a placeholder for some type.

C++:
template<typename T1, typename T2>
auto larger(T1 a, T2 b)// what is auto
{
    return a > b ? a : b;
}
This is actual code. @pbuk explained what the keyword auto means here.

C++:
template<type T> T larger(const vector<T>& data)
{
    T result{ data[0] }; //why not T result = data[0]?
    for(auto& value : data)//what is this?
    {
        if (value > result)result = value;
    }
    return result;
}
T result{ data[0]}; has the same effect as T result = data[0];
The intent is to initialize result to the value in data[0].

This type of for loop is something fairly new in C++, possibly introduced in C++11, but I'm not certain of that. It's called a range-based for loop. Here's more information - Range-based for Statement (C++) | Microsoft Docs
 
  • Like
Likes yungman
  • #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
 
  • #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
Views
5K
Replies
0
Views
318
Replies
39
Views
4K
Replies
89
Views
5K
Replies
0
Views
796
Replies
35
Views
4K
Replies
15
Views
3K
Back
Top