Boost logo

Boost Users :

From: Eric Niebler (eric_at_[hidden])
Date: 2007-10-16 11:04:07


Ovanes Markarian wrote:
> Hello Eric,
>
> this code fragment will crash, since 'what' uses iterators into the
> invalid string
>
> x::smatch what;
> if(x::regex_match(std::string(" (some_type_ptr) xyz,"),
> what, regex)) //string goes out of scope after this sentance
> {
> std::cout << what[0] << endl; // boom! string iterators are
> no longer valid
> std::cout << what[1] << endl;
> }
>
> May be there is a way to probit this kind of behaviour during the
> compilation or at least this can be stated in the docs...

As I'm sure you know, this is an invalid use. It couldn't hurt to be
explicit about it in the docs, though.

FWIW, I once found a way to disallow code like this at compile time.
This regex_match() overload takes a "std::string const &", which accepts
temporaries. It could instead take a "std::string const volatile &"
which would *not* bind to temporaries, and then I can cast away the
volatile. But it seems like a dirty trick, so I haven't done it.

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net