C++ Template Errors with different types

I need to find all the possible, at least three, errors from the following.

1
2
3
4
5
6
    template<typename A, typename B, typename C>
    C myfunction(const A& a, const B& b)
    { 
       if ( a < b )  return (C) a;
       else return (C) b;
    }


My answer was

1. typename A and B can have different types that make errors, even worse the operator < is not defined in the function.

2. typename C can be different than A and B, then it makes changes onto a, b objects which are defined const

3. typename A can be integer type but we do not use constant reference for integer type, we only use it for objects.


My third answer was wrong....

Could anybody explain why my answers were wrong and all the possible errors that can occur from this template? I got the credit for the first two answers but I do not really like them. Anybody has better solutions for this?


Thanks,


You can have a constant reference to an integer type. Constant references are usually used to save memory usage when there are large object arguments. Constant reference is not usually used for primitive types because the boost in efficiency is insignificant.

You could have also explained that the function is attempting the typecast objects a and b into C without knowing if A,B, or C are of the same class hierarchy, e.g. if C is a parent class of A and B.

Correct me if I'm wrong veteran programmers.
Last edited on
Topic archived. No new replies allowed.