template and copy constructor error finding

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!

> But I still need two more possible cases... Please help me.

In
1
2
template<typename A, typename B, typename C>
C mymin (const A& a, const B& b) ; 
type C can't be deduced; so all types have to be explicitly specified.

Run this program, look at its output and you should be able to identify a few more things that could go wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

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;
}

int main()
{
    double val1 = 66.8, val2 = 75.4 ;

    std::cout << "min of " << val1 << " and " << val2 << " is: "
               << mymin<int,double,double>( val1, val2 ) << '\n' ;

    std::cout << "min of " << val1 << " and " << val2 << " is: "
               << mymin<double,int,double>( val1, val2 ) << '\n' ;

    std::cout << "min of " << val1 << " and " << val2 << " is: "
               << mymin<double,double,char>( val1, val2 ) << '\n' ;

    char a[] = "12345", b[] = "abcde" ;
    std::cout << "min of " << a << " and " << b << " is: "
               << mymin<char*,char*,char*>( a, b ) << '\n' ;

    char c[] = "abcde", d[] = "12345" ;
    std::cout << "min of " << c << " and " << d << " is: "
               << mymin<char*,char*,char*>( c, d ) << '\n' ;
}


http://ideone.com/tRWCX6
Topic archived. No new replies allowed.