Вопрос

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

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,

Это было полезно?

Решение

A cast from A to C or B to C either needs to be polymorphic or a compatible type. You may use const with an integer reference.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top