cannot convert type string to bool - page 2

 
gangsta1: Thank you very much Alain! Solved the error perfectly. :-)
How is it that when @honest_knave gave you a correct and much more complete answer in the first reply to your query where he states (Check point #5 here) and yet, only now when @Alain Verleyen gives it again (only partially), do you acknowledge it?

Don't tell me that it was just too troublesome to follow the link and actually read the explanation?
 
template<typename T>
void Input::Update(const string value)
  {
   if(typename(T)=="bool")
      m_value  = value=="true";
   else
      m_value = (T)value;
  }


I'm having this issue when T is type bool. But actually isn't an implicit conversion. it shouldn't get into "else" condition.

 
Samuel Manoel De Souza #:

I'm having this issue when T is type bool. But actually isn't an implicit conversion. it shouldn't get into "else" condition.

Yes because it's not resolved at compile time. Suggestion:

protected:
   void Input::Update(T &val, const string str) { val = (T)str; }
   void Input::Update(bool &val, const string str) { val = str=="true"; }
public:
   void Input::Update(const string str) { Update(m_value, str); }
 
lippmaje #:

Yes because it's not resolved at compile time. Suggestion:

doesn't work, because T includes bool type. it gives the error "function already defined".

 

Yeah. Looks like you have to expand it to all specific types that you want to support.

protected:
   void Input::Update(bool &val, const string str) { val = str=="true"; }
   void Input::Update(string &val, const string str) { val = str; }
   ...
public:
   void Input::Update(const string str) { Update(m_value, str); }

Defeats the purpose of a template somewhat, but better than nothing.

 
Samuel Manoel De Souza #: doesn't work, because T includes bool type. it gives the error "function already defined".

What you want is Partial Template Specialization, which MTx does not have.

 
Samuel Manoel De Souza #:


I'm having this issue when T is type bool. But actually isn't an implicit conversion. it shouldn't get into "else" condition.

Then there is nothing left then to accept MTx doesn't support Partial Template Specialization.

As a "workaround" I don't use bool's for variables that calls generic templates. Instead I use int. And you can assign "true" and "false" to int variables as well and evaluate int variables as if they were booleans. So, other then strong data-typing, semantically there is not much lost.

It may look dirty, but it's the most elegant solution I could think of without sacrificing the use of generic functionality.

 
Dave Bieleveld #:

Then there is nothing left then to accept MTx doesn't support Partial Template Specialization.

As a "workaround" I don't use bool's for variables that calls generic templates. Instead I use int. And you can assign "true" and "false" to int variables as well and evaluate int variables as if they were booleans. So, other then strong data-typing, semantically there is not much lost.

It may look dirty, but it's the most elegant solution I could think of without sacrificing the use of generic functionality.

Nope, it has actually been fixed. - The problem was the occurance order of signatures. - if the bool type was specified before the template, it would have worked. - This bug has been fixed, actually.

Today you can have exact type signatures and a template next to each other, and the template will only match, if the exact type is not provided. - Though, only possible, if the template is function specific and not struct/class defined scope.

EDIT:

Referring to this post: https://www.mql5.com/en/forum/171214/page2#comment_26231332

Example:

protected:
   template<typename CT>
   void Input::Update(CT &val, const string str) { val = (CT)str; }
   void Input::Update(bool &val, const string str) { val = str=="true"; }
public:
   void Input::Update(const string str) { Update(m_value, str); }

cannot convert type string to bool
cannot convert type string to bool
  • 2017.03.01
  • www.mql5.com
This was fine before new mt4 builds but is throwing up "cannot convert type string to bool" when I try to compile now, any ideas...