Type cast

lotto

New member
how do you do explicit type casts in 11l?

We have a few functions like

Code:
11l       --> cpp
Int("54") --> to_int("54")
Int(54.2) --> to_int(54.2)
defined in String.hpp, but those are all manually defined functions.

But is there an equivalent of static_cast<> in 11l? For example, how could I explicitly cast my Regular Expression match to Bool?
in re.hpp there's
Code:
    class Match
    {
        friend class RegEx;
        std::wsmatch m;
    public:
        explicit operator bool() const {return !m.empty();}
        ...
    }
so I guess it's technically doable, or you wouldn't have bothered adding it. But I couldn't figure how to do it.
 

alextretyak

Administrator
Staff member
how do you do explicit type casts in 11l?
Currently, only Python-like explicit type casts are supported (e.g. Int("54") or String(54)).

For example, how could I explicitly cast my Regular Expression match to Bool?
Why do you need this?

in re.hpp there's
...
explicit operator bool()
explicit operator bool() is a replacement of the old safe bool idiom, and it is called in if (match) ... (and in if ((bool)match) ... of course, but it makes no sense to write code like this).
 

alextretyak

Administrator
Staff member
You need not explicitly cast regular expression match to Bool to check if there was any match.
Here is an example from here:
Code:
      I re:‘^[a-z]{3,10}’.match(s)
         words.append(s)
And of course you can write this:
Code:
      V match = re:‘^[a-z]{3,10}’.match(s)
      I match
         words.append(s)
         print(match.start())
 
Last edited:
Top