libc++ patch for LWG Issue 2232 - dangling reference for regex_iterators

Marshall Clow mclow.lists at
Wed Feb 19 09:17:23 PST 2014

On Feb 14, 2014, at 4:31 PM, David Blaikie <dblaikie at> wrote:
> On Fri, Feb 14, 2014 at 4:21 PM, Marshall Clow <mclow.lists at> wrote:
> Users can write
>         for(sregex_iterator i(s.begin(), s.end(), regex("meow")), end; i != end; ++i)
> binding a temporary regex to const regex& and storing a pointer to it.
> This will compile silently, triggering undefined behavior at runtime.
> Fixing this involves defining constructors for the various regex iterator types from rvalue regexes, and then marking them as “deleted”.
> And tests.
> Would this break (what I assume is) correct code doing things like:
> std::find(sregex_iterator(s.begin(), s.end(), regex("meow")), end, "foo”);  ?

(Recapping a discussion on IRC)

Yes, it would.
And though that code is "technically correct” it (a) is dangerous, and (b) doesn’t do anything useful.

The return result from std::find is an interator into a destroyed object, so you can’t really do anything with that.

The “fix” for this code is simple:

	regex re{“meow”}
	std::find(sregex_iterator(s.begin(), s.end(), re), end, "foo”); 

and now the return value of std::find is valid (until the end of the lifetime of “re”).


for the changes to the (C++14) standard.

— Marshall

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the cfe-commits mailing list