0

I have 3 functions in a header file

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

This is the objective function with which I am trying to match parameters to the arguments

char *minD(char * a, char * b)
{
    if (strcmp(a, b) > 0)
    {
        return b;
    }
    else
    {
        return a;
    }
}

This is a function that has to call both functions written above based on the parameter types.

template <typename T>
const T &minD(T const &a, T const &b, T const &c)
{
    return minD(minD(a, b), c);
}

My goal is if the given arguments in the above function match the char* type, the minD with char* type arguments has to be called. If the argument type is any other type than it should call the 1st function which has template type arguments.

The first thing I did was tried calling the minD(minD(&a, &b), &c) and change the function2 to const char *minD(const char * a, const char * b), but the compiler says cannot convert const T * to const char *. The second thing I did was tried changing the parameter type of the 2nd function to const char * minD(const char& a, const char& b) and called function with minD(minD(a, b), c), but then compiler throws an error about possible data loss from const T to const char conversion.

One thing to note here is that I cannot change the parameter type of

template <typename T>
const T &minD(T const &a, T const &b, T const &c)

Sample function calls:

val = minD(42, 7, 68);

This call is a function call to the 3rd function and then the 3rd function has to call 1st function in order to get the right answer.

const char *  const s0 = "CSC";
const char *  const s1 = "461";
const char *  const s2 = "Optimized C++";
s = minD(s0, s1, s2);

This call is a function call to the 3rd function and then the 3rd function has to call 2nd function in order to get the right answer.

I am not able to understand what am I doing wrong here. Please help.

Abhishek Panjabi
  • 439
  • 4
  • 23
  • 1
    Why are you not using `const char *` in `minD`? – 1201ProgramAlarm May 25 '20 at 18:02
  • Oh I am. That was my first try as I explained above. It throws an error that cannot convert const T * to const char *. – Abhishek Panjabi May 25 '20 at 18:05
  • 1
    I mean `const char *minD(const char * a, const char * b)`. – 1201ProgramAlarm May 25 '20 at 18:06
  • A `const T` can't be implicitly converted to `T`. In your case, `const char*` can't be converted to `char*`. – Some programmer dude May 25 '20 at 18:07
  • @cigien . It works because it always matches the arguments to 1st function. The moment you comment that out. It will throw errors right away. – Abhishek Panjabi May 25 '20 at 18:07
  • Also, having `char*` as arguments to your function overload makes no sense, since it means you can't use it with literal strings. – Some programmer dude May 25 '20 at 18:09
  • @cigien The name has templates because my program is trying to convert T type arguments to match T and char * type simultaneously. – Abhishek Panjabi May 25 '20 at 18:11
  • @Someprogrammerdude I do not care about any other types of arguments, All I care about is the two sample function calls have to call appropriate functions. – Abhishek Panjabi May 25 '20 at 18:13
  • I added some more comments for clarification as to what I am expecting. – Abhishek Panjabi May 25 '20 at 18:16
  • 1
    The problem is that in the function `template const T &minD(T const &a, T const &b, T const &c)` you *can't* call `char *minD(char * a, char * b)`, because the arguments in the template function is of type `const char*` which can't be converted to `char*`. You need to change the string overload of `minD` to take `const char*` arguments. – Some programmer dude May 25 '20 at 18:23
  • @Someprogrammerdude I already updated the question where I explain this situation in the paragraph that starts with "The first thing I did..." – Abhishek Panjabi May 25 '20 at 18:27
  • Please reproduce your compiler error messages verbatim. Anyway, you cannot both return a reference and not return a reference from the same function. Not without making an explicit effort to do so. – n. m. could be an AI May 25 '20 at 18:37
  • Well then that's another problem because if the argument `c` is a `const char*`, then `&c` will be a `const char**`. You shouldn't be using the address-of operator when passing the arguments to `minD`. Perhaps you misunderstand the use of `&` in different contexts? – Some programmer dude May 26 '20 at 05:15

1 Answers1

0

After banging my head against the wall for a long time I was able to come up with a solution.

It turns out that there is probably no way to modify these 3 given functions to match both scenarios.

So, my solution was to add an overloaded method of minD:

template <typename T>
const T* minD(const T* a, const T* b, const T* c)
{
    return minD(minD(a, b), c);
}

Also, I changed the signature of minD with two char * parameters like this:

const char *minD(const char * a, const char * b)

Now, on this call

val = minD(42, 7, 68);

It matches the minD function with 3 const T& parameters and from inside it calls minD with 2 const T& parameters. However, on this call

const char *  const s0 = "CSC";
const char *  const s1 = "461";
const char *  const s2 = "Optimized C++";
s = minD(s0, s1, s2);

It matches the newly added minD function with 3 char* parameters, which in turns calls the updated minD function with 2 char* parameters.

Hence, it solves this problem.

Abhishek Panjabi
  • 439
  • 4
  • 23