2

I was going through a textbook and found a question regarding templates.

Q. Rewrite this function using templates to work with any type. List the operations that any type using this template function must support

int FindLargest(const int& a, const int& b) { int ret; if (a > b) { ret = a; } else { ret = b; } return ret; } 

My answer:

template<typename T> T FindLargest(const T& a, const T& b) { T ret; if (a > b) { ret = a; } else { ret = b; } return ret; } 

It was easy to answer first part(rewrite using template..) but I am confused with the second sentence of the question. what is it trying to say?

7
  • 1
    what operations/functions are you applying to a and b? Commented Jan 7, 2018 at 3:39
  • 1
    If a user were to call FindLargest, how would they go about verifying that the type they pass is valid? (Without actually calling it and watching for compiler errors.) This information is present in high-quality documentation of the function. Commented Jan 7, 2018 at 3:41
  • Which operators does your function apply to a, b and ret ? Commented Jan 7, 2018 at 3:57
  • Would FindLargest work if I passed it two output streams like cout? Why or why not? Commented Jan 7, 2018 at 4:01
  • What if a and b have different types (e.g. FindLargest(1.8, 5))? Commented Jan 7, 2018 at 4:35

2 Answers 2

2

List the operations that any type using this template function must support

This part of the question is asking for what generic programming people call type constraint or concept.

Let's take a look at this line:

if (a > b) { 

This line is comparing a and b using operator >. That means type T must support the > comparison. Otherwise, the code does not compile.

As a quick experiment, you may try to declare an empty class and instantiate the function template with it.

class Empty {}; int main() { Empty x, y; FindLargest(x, y); } 

The compiler may emits an error message like:

error: no match for 'operator>' (operand types are 'const Empty' and 'const Empty')

So, one of several items in your list should be the larger-than comparison operation using >. Also note that this function template also requires other operations. I will leave it to you to find out.

Sign up to request clarification or add additional context in comments.

2 Comments

@DanielLangr Are you answering the last two sentences? Or you haven't read them? It's hard to tell, you know, text does not convey tone properly.
You're absolutely right, I was a bit tired when was reading your answer. Sorry about it, will delete my comment...
0

2) is asking you which things the type T needs to be able to do for the template to compile and operate correctly...

Consider:

T ret; 

For the above to work, T must have a default constructor.

if (a > b) { 

For the above to work, there must be some > operator that can - directly or indirectly - compare two instances of the type, returning something that either is of boolean type, or can be converted thereto. The two most obvious examples of direct support would be either an bool T::operator>(const T& rhs) const member function or a stand-alone bool operator>(const T& lhs, const T& rhs) function. An example of indirect support is an T::operatorsometype() const conversion operator to a type - such as double - that itself can be compared with >.

 ret = a; 

For the above to work, there needs to be a (copy / non-move) assignment operator.

Continue this style of analysis to fully answer the question.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.