Alec Bizan: Any idea how I can fix this?
-
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);
- 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";
William Roeder:
-
Stop using complicated (unnecessary) code, when simple is just fine.
- Bool can only be true or false. It can not be NULL;
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.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi guys. I have this function template called to_type that I use to convert a string to other data types:
It works fine when I use it for all types considered in the function, except for bool. When I run
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
gives me 'to_type' - function already defined and has different type compilation error. Any idea how I can fix this?