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.