Q. Explain at least three things that can go wrong
1 2 3 4 5 6 7 8
|
template<typename A, typename B, typename C>
C mymin (const A& a, const B& b)
{
if ( a < b )
return (C) a;
else
return (C) b;
}
|
The following is all I can think of the possible errors.
1. " < operator is not defined so we cannot compare the two objects with < operator" (X)
---> It is unrelated, since even if both types are the same there still could be no operator< for them
------------------------------------
2. "If G has a different type than T or F
, type casting occurs that changes x or y that is defined constant" (X)
---> Even if C is the same, there still will be constness loss.
Casting doesn't change the source object unless the cast invokes a very strangely defined operator X or converting constructor. Also, the lack of constness doesn't affect the source object unless C is a reference type (where it still doesn't really affect the source object until someone "mis"-uses the resulting reference)
------------------------------------
3. "Type casting from T or F to G is not guaranteed
, because the conversion constructor is not defined." (X)
-----> Typecasting itself is guaranteed, but with unpredictable results in case if types differ.
------------------------------------
4. "Copy constructor for T and F is not defined.
Therefore we are just returning the shallowly copied one.
Since it is a shallow copy
, the shallow copy can be changed
, and the original copy that is supposed to be constant can be changed too." (X)
-----> There is no need for T or F to have copy constructor.
The only constructor involved would be copy constructor of C that will accept already C style cast C as parameter.
------------------------------------
As you see, every of which is proven wrong.
Then what could be the errors on this?
I need at least three but I can't think of it anymore...
I am very confused here.
One answer I can think of is :
Types of T and F can be different, and the < operator is not defined for that case.
But I still need two more possible cases... Please help me. Thanks!