function template issue

 

Hi guys. I have this function template called to_type that I use to convert a string to other data types:

template<typename T> T to_type(string str_val)
{
    T val;

    if ( (typename(T) == "int") || (typename(val) == "long") )
        val = StringToInteger(str_val);

    if ( typename(T) == "double" )
        val = StringToDouble(str_val);
    
    if ( typename(T) == "float" )
        val = (float)StringToDouble(str_val);

    if (typename(T) == "string")
        val = str_val;

    if (typename(T) == "bool")
    {
        StringToLower(str_val);
        if ( str_val == "true" )
            val = true;
        else if ( str_val == "false" )
            val = false;
        else
            val = NULL;
    }

    if (typename(T) == "datetime")
        val = StringToTime(str_val);

    return val;
}

It works fine when I use it for all types considered in the function, except for bool. When I run

bool x = to_type<bool>("true");

I get compilation error cannot implicitly convert type 'string' to 'bool' on line 15 (Sorry I'm not sure how to insert line numbers in the above code block), which is the line that will never run when bool is used, but is problematic in the compilation process. In C++ I could specialize the template for bool to fix this. But what is called specialization in mql4 doesn't work here, because the specialized version of the function is only different from the original one in return type . So making a function like

bool to_type(string str_val)
{
    StringToLower(str_val);
    if ( str_val == "true" )
        val = true;
    else if ( str_val == "false" )
        val = false;
    else
        val = NULL;
    return true;
}

gives me 'to_type' - function already defined and has different type compilation error. Any idea how I can fix this?

 
Alec Bizan: Any idea how I can fix this?
  1. Stop using complicated (unnecessary) code, when simple is just fine.

    string   s = …;
    int      i = int(s);
    long     l = long(s);
    double   d = double(s);
    //string s = s;
       string lc=s; StringToLower(s)
    bool     b = lc == "true";
    datetime t = datetime(s);

  2. Alec Bizan: cannot implicitly convert type 'string' to 'bool'
    Bool can only be true or false. It can not be NULL;
    if ( str_val == "true" )
        val = true;
    else if ( str_val == "false" )
        val = false;
    else
        val = NULL;
    return true;
    return str_val == "true";
    
              Increase Order after stoploss - MQL4 programming forum #1.3
 
William Roeder:
  1. Stop using complicated (unnecessary) code, when simple is just fine.

  2. Bool can only be true or false. It can not be NULL;
              Increase Order after stoploss - MQL4 programming forum #1.3

Do you mean stop using the whole idea of a function template, or just use your code inside the function instead of mine?  If later, then what should I do with i, l, d, b and t variables afterwards? What should the function return?

Also, a bool variable can definitely get NULL values. The code is written so that it returns NULL (as a flag for wrong input), if the input string is neither "true" nor "false". Your suggested code doesn't do this. The last line of my code is wrong though; the function should return val, not true.

Reason: