- #36
pbuk
Science Advisor
Homework Helper
Gold Member
- 4,575
- 2,880
I am taking the time to help you, take the time to answer my questions.
I thought I answer, the two lines has to go together, so you have to write template<class T> for every function declaration.pbuk said:I am taking the time to help you, take the time to answer my questions.
1) If I am correct in post 35, then (a) one for each function, you can have two function and use two.pbuk said:I'll make it easier for you, multiple choice:
Q1. If you want two function templates how many times do you think you would have to use the keyword "template"? (a) one (b) two (c) some other number
Q2. How many times are you using the keyword "template"? (a) one (b) two (c) some other number
I know what you want me to say, it's one. But I don't know why. I thought this is ok also.pbuk said:"one for each function" is not an option. The options are (a) one (b) two (c) some other number.
template<class T> T larger(T &a, T &b){}
template<class T> voide swapVars(T &a, T &b){}
template<class T1, class T2>
T1 larger(T1 &a, T1 &b){}
void swapVars(T2 &a, T2 &b){}
That is because the problem is not in the books, it is in your head. I can help you solve this but only when you accept that the way to learn is not to present me with what you think you know or what you think you want me to hear, it is simply to answer the question. One letter will do, a, b or c:yungman said:If this is not correct, I am still missing something that I don't understand and I cannot find it in the books.
template
? (a) one (b) two (c) some other number?It is one.pbuk said:That is because the problem is not in the books, it is in your head. I can help you solve this but only when you accept that the way to learn is not to present me with what you think you know or what you think you want me to hear, it is simply to answer the question. One letter will do, a, b or c:
If you want two function templates how many times do you think you would have to use the keywordtemplate
? (a) one (b) two (c) some other number?
template
twice.The format for declaring function templates with type parameters is:
orC++:template <class identifier> function_declaration;
C++:template <typename identifier> function_declaration;
Thanks, I have to spend some time reading it. I'll be back.pbuk said:Ok, that is wrong. If you want two function templates you have to use the keywordtemplate
twice.
I have quite a lot more to give you here, but here is a start as to why: (taken from http://www.cplusplus.com/doc/oldtutorial/templates/ - I will try and get a more up to date source)So each function declaration requires its own 'template' keyword and a class or typename identifier as appropriate. The class or typename identifier only has a meaning within the function definition with which it is associated (i.e. that is its scope).
template
keywordtemplate <class myType>
myType GetMax (myType a, myType b) {
return (a>b?a:b);
}
template <class myType> myType GetMax (myType a, myType b) { return (a > b ? a : b); }
template
<class myType>
myType GetMax (myType a, myType b)
{
return (a > b ? a : b);
}
template
<class
myType>
myType
GetMax (
myType
a, // a is the first argument
myType
b // b is the second
)
{
return
(
a > /* that sign means greater than */ b
? a
: b
);
}
#
as the first character of a new line, and in //
style comments which are terminated by the first following newline.template <class identifier> function_declaration;
// or
template <typename identifier> function_declaration;
template
keyword, followed by a class or typename identifier, followed by a function declaration.#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;
}
if(a>b) return a;
else return b;
I call it a ternery expression although there are other names: https://www.programiz.com/cpp-programming/ternary-operatoryungman said: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.
In this case I think I agree with you - it is clearer and it doesn't make the line too long.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.
#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;
}
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.yungman said:But I have questions on the "short" form of writing statements like the a>b ? a:b type.
int pets = 2;
std::cout << "I have " << pets << " pet" << (pets > 1 ? 's' : '\0') << std::endl;
Yes, I understand that already, it's the other news ones in my post.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:
If the Boolean expression is true, the conditional expression evaluates to exp1.
- The first part (a > b above) is a Boolean expression, one that evaluates to true or false.
- The second part (exp1).
- The third part (exp2).
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.
Maybe it's some of these:yungman said:Yes, I understand that already, it's the other news ones in my post.
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;
}
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 tell me in this case, cheapness doesn't pay. I downloaded Gaddis 6th edition for free, it has nothing of this kind at all.
Not sure which simplified code you're asking about. Do you mean the braced initializer list (e.g.,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:Don't they have a general name for this kind of simplified code.
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.yungman said:I just want to find a website that show the translation.